0
for i in `cat filename.lst|awk '{print $1}{print $2 > var1}'`
do
echo "$i and $var1"
done

but not working , i have file as

xyz 123
abc 456
pqr 789

expected output:-

xyz and 123
abc and 456
pqr and 789
Til
  • 5,150
  • 13
  • 26
  • 34
Roger
  • 85
  • 1
  • 7

2 Answers2

2

You seem to be looking for something like

while read -r i value; do
    var1=$value
    echo "$i and $var1"
done <filename.lst

You want to avoid reading lines with for and the useless use of cat.

Awk cannot manipulate your Bash variables, and Bash has no idea what happens inside your Awk script.

If you absolutely insist on using Awk for this, you want its output to be a string you can safely eval from your shell.

eval "$(awk '{ print "var1=\047" $2 "\047;\n" \
    "echo \"" $1 " and \$var1\"" }' filename.lst)"

The precise definition of "safely" is probably too hard to pin down exactly. In other words, really just don't do this.

tripleee
  • 175,061
  • 34
  • 275
  • 318
0
$ awk '{print $1, "and", $2}' file
xyz and 123
abc and 456
pqr and 789

$ sed 's/ / and /' file
xyz and 123
abc and 456
pqr and 789

If that's not all you want then edit your question to clarify your requirements and provide a more truly representative example.

Ed Morton
  • 188,023
  • 17
  • 78
  • 185
  • 1
    hi Ed I would like to use a loop , to read line by line, I read 1st field (xyz )while i assign 2nd field (123) to a variable(lets say var1) from this filename.lst. so that i can use this 2nd field inside the loop to do some other stuff. – Roger Feb 08 '19 at 08:34
  • @Roger if the "other stuff" doesn't involve creating files or processes (i.e. the stuff a shell is for) then you may be going down the wrong path, see https://unix.stackexchange.com/q/169716/133219. – Ed Morton Feb 08 '19 at 17:24