Repeat same word in the rest of the column.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Repeat same word in the rest of the column.
# 1  
Old 03-24-2016
[SOLVED] Repeat same word in the rest of the column.

My input is:
Code:
a.txt computer
b.txt
c.txt
e.txt

I want my output to be:
Code:
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.

Last edited by shoaibjameel123; 03-24-2016 at 06:36 AM..
# 2  
Old 03-24-2016
Hello shoaibjameel123,

Could you please try following and let me know if this helps you.
Code:
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
This User Gave Thanks to RavinderSingh13 For This Post:
# 3  
Old 03-24-2016
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.
# 4  
Old 03-24-2016
Try also
Code:
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.
This User Gave Thanks to RudiC For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to search for a word in column header that fully matches the word not partially in awk?

I have a multicolumn text file with header in the first row like this The headers are stored in an array called . which contains I want to search for each elements of this array from that multicolumn text file. And I am using this awk approach for ii in ${hdr} do gawk -vcol="$ii" -F... (1 Reply)
Discussion started by: Atta
1 Replies

2. UNIX for Advanced & Expert Users

Cut a word between two strings and repeat the same in the entire file

in the below data i need to search for the word typeMismatch and then traverse back to find the filename of that particular mismatch. Like this we have to get all the file names which has error in them. How can i acheive this. I tried use sed or awk but not able to achevie the same. Sample... (2 Replies)
Discussion started by: ATWC
2 Replies

3. Shell Programming and Scripting

Find Word in Column with replace (2)

Dear ALL, I have sample file : 4c:66:41:6b:b5:5f, 00:00,00:00, -22:-25, robert, 10.101.3.119, host1 4c:66:41:2c:c1:5a, 00:00,00:00, -21:-25, 10.101.3.112, host1 4c:66:41:6b:b1:5a, 00:00,00:00, -21:-25, Julia, 10.101.3.113, host1 4c:66:41:2c:c1:5b, 00:00,00:00, -21:-25, 10.101.3.115, host1... (2 Replies)
Discussion started by: gnulyn
2 Replies

4. Shell Programming and Scripting

Find Word in Column with replace

Hi ALL i have file.txt with text : 4c:66:41:6b:b5:5f, 00:00,00:00, -22:-25, users1, 10.101.3.119, host1 4c:66:41:2c:c1:5a, 00:00,00:00, -21:-25, 10.101.3.112, host1 4c:66:41:6b:b1:5e, 00:00,00:00, -21:-25, users1, 10.101.3.113, host1 4c:66:41:2c:c1:5b, 00:00,00:00, -21:-25, 10.101.3.115,... (3 Replies)
Discussion started by: gnulyn
3 Replies

5. Shell Programming and Scripting

Add word for a missing column

Hi team... I need some help/advise on adding a word for a missing column. I have a file as below. Rep N1 Forever Limited 2015 Rep N2 Limited 2016 since 2nd content is 3 lines missing one line so i need to make this like Rep N1 Forever (3 Replies)
Discussion started by: newbee5
3 Replies

6. UNIX for Dummies Questions & Answers

Sum third column in a file when 1 and 2 repeat

Hello, Below is a portion of a file I am working on: 2:15 3001 120 2:15 3001 26.25 2:15 3002 12.5 2:15 3002 15 9:45 3001 45 9:45 3001 45 9:45 3002 105 9:45 3005 180 9:45 3005 123.75 9:45 3005 15 I am trying to get the sum of column 3 for all repeated values of columns 1 and 2 (The... (6 Replies)
Discussion started by: c@rlos
6 Replies

7. Shell Programming and Scripting

Help with subtraction column by column and repeat print out with N

Input file: 2 10 15 20 24 Output file 2 8 NNNNNNNN 10 5 NNNNN 15 5 NNNNN 20 4 NNNN 24 Do anybody experience subtraction column by column and print out number of subtraction times with N? The second column of the output file is the subtraction of 10-2 = 8; The third column just... (3 Replies)
Discussion started by: perl_beginner
3 Replies

8. UNIX for Dummies Questions & Answers

Search word in 3rd column and move it to next column (4th)

Hi, I have a file with +/- 13000 lines and 4 column. I need to search the 3rd column for a word that begins with "SAP-" and move/skip it to the next column (4th). Because the 3rd column need to stay empty. Thanks in advance.:) 89653 36891 OTR-60 SAP-2 89653 36892 OTR-10 SAP-2... (2 Replies)
Discussion started by: AK47
2 Replies

9. Shell Programming and Scripting

To search a word in particular column using awk

I have a data in a file like this 1 praveen bmscollege 2 shishira bnmit 3 parthiva geethamce I want to search "praveen" using awk command i tried like this but i did not get awk `$2="praveen" {print $0} ` praveen.lst can anyone help me solving this problem in... (2 Replies)
Discussion started by: praveenhegde
2 Replies

10. Shell Programming and Scripting

How to grep a word and next column to that word?

Hi, I have input file as below. Can you help me? inac_4y;0;2;Balance;200;1;1; 0;2;Balance;100;1; 0;inac_nq;0;1;Balance;100;1 desired output Balance;200 Balance;100 Balance;100 -Suresh Please use and tags when posting code, data or logs etc. to preserve formatting... (5 Replies)
Discussion started by: suresh3566
5 Replies
Login or Register to Ask a Question