Add word for a missing column


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Add word for a missing column
# 1  
Old 10-03-2015
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.

Code:
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

Code:
Rep N1
Forever
Limited
2015

Rep N2
Healthy
Limited
2016

so i did like this

Code:
$ cat x.log |xargs -n 5
Rep N1 Forever Limited 2015
Rep N2 Limited 2016

but i want Healthy after 2nd column... i used awk if else case but its not working...

like i searched for each like for column 3 if its forever keep it as is i.e
$3 == "Forever" print $3
else
print "healthy"

end of the day i want them to be same column count like line 1 has 5 cols same like i want line 2 also 5 cols.

but its not working... need experts help please.

---------- Post updated at 09:45 AM ---------- Previous update was at 09:33 AM ----------

tried this way but am not getting whole file.

Code:
$ cat x.log |xargs -n 5|awk '{ if ( $3 !~ /Forever/ ) {printf( "%s\n%s\n", "Healthy", $3 );} else {print $3;}}'
Forever
Healthy
Limited

# 2  
Old 10-03-2015
Give it a try to any of these
Code:
perl -00  -paF'\n' -e 's/Limited/Healthy\n$&/ if $#F == 2' newbee5.file

Code:
awk 'NF==3 {$2="Healthy" FS $2}1' FS="\n" OFS="\n" RS=  ORS="\n\n" newbee5.file

This User Gave Thanks to Aia For This Post:
# 3  
Old 10-03-2015
Thanks Aia it worked out.

i like awk more. Can you just explain the logic with awk. so that way it can be miore helpful.
# 4  
Old 10-03-2015
Code:
awk '
   NF==3 {$2="Healthy" FS $2}1'  # if the Number of Fields are 3 add to field 2 the string Healthy + Field Separator + $2
                                 # 1 makes awk to print $0 for those records which meet the previous condition
                                 # any other records that do not need to be modified the default is to be displayed.
FS="\n"                       # set Field Separator to newline
OFS="\n"                      # set Output Field Separator to newline
RS=                           # set Record Separator to paragraph mode
ORS="\n\n"                    # set Output Record Separator to produce a paragraph
newbee5.file

This User Gave Thanks to Aia 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. Shell Programming and Scripting

Help with awk script to get missing numbers in column 1

Hello to all, I have show below a file separated by commas. In first column has numbers where the last number is 13. 1,4 2,6 3,7 5,2 6,5 7,5 8,65 9,10 11,78 13,2 What I want to know is which numbers are missing from 1 to 13 (in this case 13 is last number in column 1). My real... (17 Replies)
Discussion started by: Ophiuchus
17 Replies

3. 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

4. UNIX for Dummies Questions & Answers

Merging two text files by a column and filling in the missing values

Hi, I have to text files that I want to merge by the first column. The values in the first column pretty much match for the first part. However there are some values that are present in column 1 and not present in column 2 or vice versa. For such values I would like to substitute X for the... (9 Replies)
Discussion started by: evelibertine
9 Replies

5. Shell Programming and Scripting

Fill missing numbers in second column with zeros

Hi All, I have 100 files with names like this: 1.dat, 2.dat, 3.dat until 100.dat. My dat files look like this: 42323 0 438939 1 434 0 0.9383 3434 120.23 3 234 As you can see in the second column, some numbers are missing. I want to fill those missing places with 0's in all... (3 Replies)
Discussion started by: shoaibjameel123
3 Replies

6. 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

7. Shell Programming and Scripting

Need to add letters to a column and add in a new column subtracting from another column

So I have this input 1 10327 rs112750067 T C . PASS DP=65;AF=0.208;CB=BC,NCBI 1 10469 rs117577454 C G . PASS DP=2055;AF=0.020;CB=UM,BC,NCBI 1 10492 rs55998931 C T . PASS DP=231;AF=0.167;CB=BC,NCBI 1 10583 rs58108140 G A ... (3 Replies)
Discussion started by: kellywilliams
3 Replies

8. Shell Programming and Scripting

Fix CSV file with column missing quotes

I have a CSV file that is missing quotes around a column that contains text with commas. Example: Column1, Column2, Column3, Column4, Column5, Column6 Data1, Data2, Data3, Data, 4, Data5, Data6 Data1, Data3, Data3, Data, text, 4, Data5, Data6 I think the easiest way for me to fix this is to... (2 Replies)
Discussion started by: EmptyH
2 Replies

9. Shell Programming and Scripting

how to include the missing column in the original file using awk

Hi Experts, The content of the raw file: date,nomsgsent,nomsgnotdeliver,nomsgdelay 201003251000,1000,1,2 201003251000,900,0,0 201003251000,1450,0,0 201003251000,1230,0,0 However, sometimes, the column will missing in the raw files: e.g. date,nomsgsent,nomsgdelay... (8 Replies)
Discussion started by: natalie23
8 Replies

10. UNIX for Dummies Questions & Answers

grep a word and display its column

Hi, I need idea about this, say I have this line: 05 21 * * 0,6 /user/clean.desktop.sh > /tmp/desktop_rpt 2>&1 I would need to grep the word desktop and display the /user/clean.desktop.sh and not the whole line. And if I have some more lines say, 05 21 * * 0,6 /user/clean.desktop.sh >... (1 Reply)
Discussion started by: Orbix
1 Replies
Login or Register to Ask a Question