Add field delimiter for the last field


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Add field delimiter for the last field
# 1  
Old 07-22-2010
Add field delimiter for the last field

I have a file with three fields and field delimiter '|' like:
Code:
abc|12:13:45|123
xyz|12:87:32|
qwe|54:21:09

In the file the 1st line has proper data -> abc|12:13:45|123
,the 2nd line doesnt has data for the 3rd field which is okay
, the 3rd line doesnt has data for the 3rd field as well the field delimiter before the 3rd field is also missing.

I have the requirement to add the field delimiter for the last field if the last field as well as the field delimiter is missing as with the case of the 3rd line:
the 3rd line should look like:
Code:
qwe|54:21:09|

Please help me in this?

Last edited by Franklin52; 07-22-2010 at 01:15 PM.. Reason: Please use code tags
# 2  
Old 07-22-2010
One way:
Code:
awk -F"|" '{NF=3}1' OFS="|" file

# 3  
Old 07-22-2010
Code:
$ awk -F\| 'NF == 2 { $0=$0 FS} 1' file
abc|12:13:45|123
xyz|12:87:32|
qwe|54:21:09|

# 4  
Old 07-22-2010
Code:
# sed 's/\(.*|.*:.*:[0-9][0-9]*\)$/\1|/' file
abc|12:13:45|123
xyz|12:87:32|
qwe|54:21:09|

# 5  
Old 07-22-2010
Thanks a lot friends.... this command worked :
Code:
# sed 's/\(.*|.*:.*:[0-9][0-9]*\)$/\1|/' file

but for these commands i received this error:
Code:
$ awk -F"|" '{NF=3}1' OFS="|" file
awk: syntax error near line 1
awk: bailing out near line 1
 
$ awk -F\| 'NF == 2 { $0=$0 FS} 1' file
awk: syntax error near line 1
awk: bailing out near line 1

Am I doing something the wrong way?

regards,

Last edited by Scott; 07-22-2010 at 02:20 PM.. Reason: Please use code tags
# 6  
Old 07-22-2010
Use nawk or /usr/xpg4/bin/awk on Solaris...
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How can awk ignore the field delimiter like comma inside a field?

We have a csv file as mentioned below and the requirement is to change the date format in file as mentioned below. Current file (file.csv) ---------------------- empname,date_of_join,dept,date_of_resignation ram,08/09/2015,sales,21/06/2016 "akash,sahu",08/10/2015,IT,21/07/2016 ... (6 Replies)
Discussion started by: gopal.biswal
6 Replies

2. UNIX for Dummies Questions & Answers

Find delimiter and double quote the field

Hi I have a asterisk (*) delimited file and there are some fields which contain data having asterisk , now i want to double quote the fileds which contain data with asterisk Ex: input file ID*NAME*EMAIL 1*BILL*BILL@AOL.com 2*J*OY*JOY@msn.com in the 2nd record JOY has a asterisk value in... (11 Replies)
Discussion started by: halmstad
11 Replies

3. Shell Programming and Scripting

awk output field delimiter

Dear All, 1.txt (tab in between each value in a line) a b c a b c a c d you can see below, why with ~ i can output with tab, but = cannot? # awk -F'\t' '$2 ~ /b/' 1 a b c a b c # awk -F'\t' '$2 = "b"' 1 a b c a b c a b d ... (1 Reply)
Discussion started by: jimmy_y
1 Replies

4. UNIX for Dummies Questions & Answers

Cut a field from a line having quotes as delimiter

Hi , I have extract a single field from the 2nd row of a file of which the format is as given below "Field1","Field2","Field3",...,"Field17",... I need to cut Field17 as such (no quotes required).. I give the command head -2 file_name | tail -1 | cut -d "," -f17 But the output is... (2 Replies)
Discussion started by: nivin_govindan
2 Replies

5. Shell Programming and Scripting

deplace field delimiter

hi here my problem: i have 2 file: 1.tmp 111 222 555 2.tmp 1*TEST1**111*LA 2*TEST2**112*LA 3*TEST3**222*LA 4*TEST4**333*LA 5*TEST5**555*LA (5 Replies)
Discussion started by: saw7
5 Replies

6. Shell Programming and Scripting

awk field equal something, then add something to the field

Hi Everyone, a.txt a b c 1 e e e e e a b c 2 e e e e e the output is a b c 1 e e e e e a 00b c 2 e e e e e when 4th field = '2', then add '00' in the front of 2nd field value. Thanks (9 Replies)
Discussion started by: jimmy_y
9 Replies

7. Shell Programming and Scripting

delimiter appears in field

The typical line of the input file is as follows, 123|abcde|"xyz|mn"|ghelosa|3455hello| The delimiter is |. I need to change it to another delimiter, say ~. For the above line, the output should be: 123~abcde~xyz|mn~ghelosa~3455hello~ The challenge is when | appears in a field, it... (2 Replies)
Discussion started by: derekxu
2 Replies

8. Shell Programming and Scripting

help with sed to add delimiter and new field to each row

I have a file with millions of rows that I need to add a delimiter and a new field with a zero to the end of each row. (its too big to open and do a find and replace regex) I'm looking for the next line '\n' and need to replace it with a Unit Separator (hex \037) 0 \n. I've tried the... (2 Replies)
Discussion started by: kmac
2 Replies

9. Shell Programming and Scripting

delete a field along with delimiter in the whole file

I have file with 20 fields and its pipe delimiter. I need to remove the 18th field along with pipe delimiter that seperates 17th and 18th field. In turn that means i want to make it now a file with only 19 fields. Can some body let me know how ican remove the 18th field from the whole file? (5 Replies)
Discussion started by: dsravan
5 Replies

10. Shell Programming and Scripting

add increment field when first field changes

Hi I have a file that looks like the following: Name1 aaa bbbb Name1 ffd hhghg Name1 dffg ghhhh Name2 rtrt dfff Name2 rrtfr tgtt Name2 dsddf gfggf Name2 ffffg gfgf NAme3 fdff ghhgh Is it possible to format it so that a number... (2 Replies)
Discussion started by: azekry
2 Replies
Login or Register to Ask a Question