0

I want to add a word at the end of each line in my text file which is stored in variable. whenever i execute shell script instead of concatenate content stored in variable variable itself get concatenated. Below is the example for same:

Input:

cat output2.txt

12345 

att1=Ramesh^Mumbai

awk '{print $0"^$att1"}' output2.txt >output3.txt

output:

12345^att1

Desired Output:

12345^Ramesh^Mumbai 
Draken
  • 3,134
  • 13
  • 34
  • 54
Rishabh
  • 67
  • 1
  • 9

2 Answers2

0

Try this:

awk -v att1='Ramesh^Mumbai' -v OFS='^' '{print $0,att1}'

-v option allows to pass variable to awk

OFS is the output field separator (that will replace the , in the print statement by ^)

oliv
  • 12,690
  • 25
  • 45
0
man awk:

-v var=val
Assign the value val to the variable var, before execution of
the program begins. Such variable values are available to the
BEGIN block of an AWK program.

you can use this;

#!/bin/bash
att1=Ramesh^Mumbai
awk -v att1=$att1 '{print $0"^"att1}' output.txt > output3.txt

Example;

user@host:/tmp$ cat output.txt
12345
abab
dafadf
adfaf

user@host:/tmp$, ./test.sh

user@host:/tmp$ cat output3.txt
12345^Ramesh^Mumbai
abab^Ramesh^Mumbai
dafadf^Ramesh^Mumbai
adfaf^Ramesh^Mumbai
Mustafa DOGRU
  • 3,994
  • 1
  • 16
  • 24