Repeat same word in the rest of the column.

My input is:

a.txt computer
b.txt
c.txt
e.txt

I want my output to be:

a.txt computer
b.txt computer
c.txt computer
e.txt computer

There are about 100000 text files having the same format as my input data. What I am doing now is too slow and also requires plenty of scripts.

  1. wc -l all files and store the number of lines in separate files.
  2. Use awk to print the second column in each file and store in another file.
  3. Print the word in the second column, "wc -l" times (point 2 above) in another file.
  4. Delete the second column from all input files.
  5. paste -d' ' the output of (3) and (4).

Is there any better way to approach this problem? I am using Linux with BASH.

Hello shoaibjameel123,

Could you please try following and let me know if this helps you.

for file in *.txt
do
        awk '{if($2==""){$2=Q};if($2){Q=$2};print}' $file > tmp_file
        mv tmp_file $file
done

Also if above doesn't meet your requirement please let us know complete details about your requirement to help you, hope this helps.

NOTE: Also try to execute this within TEST environment for few files before running it in actual environment if All is Well with test then go for actual environment please.

Thanks,
R. Singh

1 Like

Thanks. The output is exactly what I want, and thus it meets my requirement. The difference now is that your is a much faster way to get the output.

Try also

awk 'NF==2{Q=$2} $2=Q' file
a.txt computer
b.txt computer
c.txt computer
e.txt computer

And, overwrite the original file only when the command worked error free.

1 Like