Find field count and replace


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Find field count and replace
# 1  
Old 08-25-2011
Find field count and replace

Hello All,

I have a file with contents like
Code:
apple|ball|charlie|David|
England|France|Germany|
Ireland|Japan|King|London|
Man|Nancy|Orange|

here the column delimiter is |

so if any of the lines/rows in the file has 3 only records (last field is empty), i want to place a | at the end of the row/line, so the output should be like

Code:
apple|ball|charlie|David|
England|France|Germany||
Ireland|Japan|King|London|
Man|Nancy|Orange||

if NF=3 place | at the end.

Appreciate your help. Thanks

Last edited by Franklin52; 08-25-2011 at 03:16 AM.. Reason: Please use code tags for code and data samples, thank you
# 2  
Old 08-25-2011
Code:
 
awk -F| ' if(NF==3){print $0"|"} else {print $0}' inputfile

# 3  
Old 08-25-2011
if you have Ruby(1.9+)
Code:
$ ruby -pne '$_=$_.chomp+"|\n" if $_.count("|")<4;' file

# 4  
Old 08-25-2011
Code:
awk -F'|' 'NF==4{$0=$0 FS};1' yourFile

# 5  
Old 08-25-2011
Thank you Guys

awk -F'|' 'NF==4{$0=$0 FS};1' yourFile .... worked
awk -F| ' if(NF==3){print $0"|"} else {print $0}' inputfile ..throwed syntax error on Aix

awk -F"|" 'if(NF==3) {print $0}"|" ; else {print $0}' 1.sql
Syntax Error The source line is 1.
The error context is
>>> if <<< (NF==3) {print $0}"|" ; else {print $0}
awk: 0602-500 Quitting The source line is 1.

enclosed if and else in {}, but the out put was not the desired.

anyway the first one worked ..thank you!
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Find max length of the field and then replace zero

hai guys, pick the 1st field and calculate max length. if suppose max length is 2, then compare the all records if <2 then add zero's prefix of the record. for ex: s.no,sname 1,djud 37,jtuhe in this max length of the 1st field is 2 right the output wil be s.no,sname 01,djud... (6 Replies)
Discussion started by: Suneelbabu.etl
6 Replies

2. Shell Programming and Scripting

Find a blank field and replace values to NA

Hi All, i have a file like col1 col2 col3 13 24 NA 12 13 14 11 12 13 14 22 NA 18 26 NA in this file if i found "NA" other values in the line are also replace by NA Could you help me! (7 Replies)
Discussion started by: Shenbaga.d
7 Replies

3. Shell Programming and Scripting

To find char field and replace null

hi, i having a file with | seperated in which i need to search char in 3rd column and replace with null. i need to replace only the coulmn where character occurs in 3rd field for eg: file1.txt xx|yy|xx|12 output file: xx|yy||12 (5 Replies)
Discussion started by: rohit_shinez
5 Replies

4. Shell Programming and Scripting

Read Field from file1 and find and replace in file2

Hi All, I have file1 line below: $myName$|xxx Now I need to read the file1 and find for $myName$ in file2 and replace with xxx file1: $myName$|xxx file2: My name is $myName$ expected output in file2 after executing the script is below: my name is xxx Thanks, (8 Replies)
Discussion started by: gdevadas
8 Replies

5. Shell Programming and Scripting

Find and replace blank in the last field

Hi all, I have a huge file and I need to get ride of the fields 6-11 and replace the blanks in field 5 with a missing value(99999). 159,93848,5354,343,67898,45,677,5443,434,5545,45 677,45545,3522,244, 554,54344,3342,456, 344,43443,2344,444,23477... (12 Replies)
Discussion started by: GoldenFire
12 Replies

6. Shell Programming and Scripting

How to find the count and replace the particular part of string in perl?

Hi, I am taking the current time using localtime function in perl. For example if the time is: #Using localtime $time = "12:3:10"; I have to replace the value 3 (03) i.e second position to be 03. The output should be: 12:03:10 But if the other string for example: $str:... (1 Reply)
Discussion started by: vanitham
1 Replies

7. UNIX for Dummies Questions & Answers

Find and replace a field in the last line

I have a file 'test.out' with contents: 1|1|10|10|I|asdf| 2|1|10|10|I|sdfg| 4|1|10|10|I|hgfj| 34|0|10|10|I|sdg| I want to modify the fifth column with value 'I' to 'A' for only the last line. Below is what I expect to see: 1|1|10|10|I|asdf| 2|1|10|10|I|sdfg| ... (3 Replies)
Discussion started by: ChicagoBlues
3 Replies

8. Shell Programming and Scripting

awk: find and replace in certain field only, help needed

I got a sample file like this. $ cat test 12|13|100|s 12|13|100|s 100|13|100|s 12|13|100|s I want to replace all 100 by 2000 only in 3rd field using "awk" This is replacing all 100's :-( $ awk -F "|" '{gsub( /100/,"2000");print}' test 12|13|2000|s 12|13|2000|s 2000|13|2000|s... (5 Replies)
Discussion started by: jkl_jkl
5 Replies

9. Shell Programming and Scripting

find pattern and replace another field

HI all I have a problem, I need to replace a field in a file, but only in the lines that have some pattern, example: 100099C01101C00000000059394200701CREoperadora_TX 100099C01201C00000000000099786137OPERADORA_TX2 in the example above I need to change the first field from 1 to 2 only if... (3 Replies)
Discussion started by: sergiioo
3 Replies
Login or Register to Ask a Question