Home > Enterprise >  How do i use loops to get it to read each line at a time in bash?
How do i use loops to get it to read each line at a time in bash?

Time:06-14

I have a text file like,

01_AA_00 11
02_BB_00 11
03_CC_01 22
04_BB_01 22
05_CC_02 33
06_CC_02 33

Expected output in a new file is,

01_AA_00 11 AABBCCDD
02_AA_00 11 AABBCCDD
03_AA_01 22 AABBCCDD
04_BB_01 22 AABBCCDD
05_CC_02 33 AABBCCDD
06_CC_02 33 AABBCCDD

What i have been trying to do,

while IFS= read -r line; do
    fName=$(awk '{print $1}' $1)
    printf "$fName AABBCCDD\n" > nFile.txt
done < $1

the output i am getting is like this,

01_AA_00 11
02_BB_00 11
03_CC_01 22
04_BB_01 22
05_CC_02 33
06_CC_02 33 AABBCCDD

I not looking to just add text after the each line, where i know that it could be done like so awk '{print $0, "AABBCCDD"}' file.txt > nFile.txt since i have to use other information stored in variables.

CodePudding user response:

You can do it with awk only

awk -v v="AABBCCDD" '{print $0 " " v}' file.txt > nFile.txt

CodePudding user response:

just leverage ORS ::

{m,g}awk 1 ORS=' AABBCCDD\n'

01_AA_00 11 AABBCCDD
02_BB_00 11 AABBCCDD
03_CC_01 22 AABBCCDD
04_BB_01 22 AABBCCDD
05_CC_02 33 AABBCCDD
06_CC_02 33 AABBCCDD

another way is to use OFS instead ::

 {m,g}awk   NF FS='^$' OFS=' AABBCCDD' 

CodePudding user response:

you can just

while IFS= read -r line; do 
 echo $line" AABBCCDD" >> nFile.txt;
done < $1
  • Related