sed returns different results while substitution on a pipe delimited file

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed returns different results while substitution on a pipe delimited file
# 1  
Old 07-27-2011
sed returns different results while substitution on a pipe delimited file


Need help with a sed command that I am using to substitute 3 positions of a pipe delimited file.
i am getting different results while substituting the same position of two different files with the same value. Please see details below:

$ cat chk2

0008086172|0000000003|0000007468|MX|0000001003|0904867492|00029|00001|03000|00001|2011-07-25 18:38:47|A|00911|0000537851|0000001003|0052138123|||00005|0000|02501|0006914405||006914404|000088563128875|00002|001088563128875||||A|2011-07-13|2011-08-01|2049-12-31|2011-07-15|2011-07-13||00033|0000||00090|N|N|N|N|0002||||N||||||DM||KG|||00000025776|00000029900|EA|000010000|
$ cut -f 99,100,101 -d '|' chk2

$ sed -e 's/|/000010000|/99' -e 's/\(.*\)|.*000010000/\1|000010000/' -e 's/|/000010000|/100' -e 's/\(.*\)|.*000010000/\1|000010000/' -e 's/|/000010000|/101' -e 's/\(.*\)|.*000010000/\1|000010000/' chk1| cut -f 99,100,101 -d '|'


As seen above, i am replacing the 99th,100th,101th position of the pipe delimited file using the sed command to 000010000, but looks like it is appending 000010000 to each values.

But when i am doing the same thing on the same position for another file, it's replacing correctly:

$ cat chk2
0000336853|0000000003|0000007088|CA|0000001002|0039424552|00029|00001|03000|00001|2011-07-23 23:02:08|A|00908|0002887394|0000001002|0052248160|||00028|0000|00208|0010575385||010575384|000003600014264|00002|001003600014264|00003|001003600014264||A|2010-12-02|2010-12-31|2049-12-31|2010-12-03|2010-12-02||00020|0000||00090|N|N|N|N|0002||||N||||||IN||LB|||00000004397|00000004397|EA|001640000|EA|||N|Y|Y|||||||H|Y|000026685|N|00001|00000||00003|||HA|00120|00001|0003|N|N|0000000415400|C|0000000001|00000057607|KG|N|000015900|000008875|000016800|IN|000001371|CF|0|||00006|00005|0000000415400|0000000001|000015900|000008875|000001371|CF|000016800|IN|00000057607|KG|0000000000||R|00001|Y||0857622280|00028|00000|US||100|||||+|00035|+|00080|+|00035|+|00080||||N|N|N|||||||||||N|0516|0002||2010-12-02|||||||N|N|0028|0000|2010||00000127000||Y|||N|0000||0001||||||||||N|N|||N|N|N|N||
$ cut -f 99,100,101 -d '|' chk2


$ sed -e 's/|/000010000|/99' -e 's/\(.*\)|.*000010000/\1|000010000/' -e 's/|/000010000|/100' -e 's/\(.*\)|.*000010000/\1|000010000/' -e 's/|/000010000|/101' -e 's/\(.*\)|.*000010000/\1|000010000/' chk2 | cut -f 99,100,101 -d '|'


Here, the 99th,100th and 101th position is replaced with 000010000.
Just wondering why it is not updated in the first file.?
Anything wrong with the sed command that's used?
Any help is appreciated.

# 2  
Old 07-27-2011
Your "sed" will replace the "|" with "000010000" on the 99 position:

Data before (123 is field 99):

Running your "sed":
 sed -e 's/|/000010000|/99'

Data after your "sed":

You can do it in "sed" using a data mark:

sed -e 's/|/MyMark/99' -e 's/\(.*|\).*MyMark/\1||/' Input_File

The above will remove the value in the field 99.
# 3  
Old 07-28-2011
But I still don't understand why the second example replaces the 99th,100th and 101th position.

My requirement is to replace n positions of a pipe delimited file with a value i specify.
The example you provided is to delete right?
Is there a way to accomplish this?

Thanks in advance,
# 4  
Old 07-28-2011
Originally Posted by Shell_Life
You can do it in "sed" using a data mark:

sed -e 's/|/MyMark/99' -e 's/\(.*|\).*MyMark/\1||/' Input_File

The above will remove the value in the field 99.
The only "data mark" needed is already provided, the field delimiter, "|":
sed 's/[^|]*|/|/99'

---------- Post updated at 09:42 AM ---------- Previous update was at 09:31 AM ----------

Originally Posted by vmenon
My requirement is to replace n positions of a pipe delimited file with a value i specify.
To replace the value of the nth field in a pipe-delimited line, where n=99:
sed 's/[^|]*|/NEWVALUE|/99'

That will not work for the final field since it is not followed by a pipe. If that case needs to be handled, use a '$' anchor instead of a '|' at the end of the regular expression.

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Need to convert a pipe delimited text file to tab delimited

Hi, I have a rquirement in unix as below . I have a text file with me seperated by | symbol and i need to generate a excel file through unix commands/script so that each value will go to each column. ex: Input Text file: 1|A|apple 2|B|bottle excel file to be generated as output as... (9 Replies)
Discussion started by: raja kakitapall
9 Replies

2. Shell Programming and Scripting

Insert a value in a pipe delimited line (unsig sed,awk)

Hi, I want to insert a value (x) in the 3rd position of each line in a file like below a|b|c|d|1 a|b|c|d a|b|c|d|e|1 a|b|cso that output file looks like a|b|x|c|d|1 a|b|x|c|d a|b|x|c|d|e|1 a|b|x|cI can do that using perl as below #!/usr/bin/perl -w use strict; #inserting x at... (5 Replies)
Discussion started by: sam05121988
5 Replies

3. Shell Programming and Scripting

How to ignore Pipe in Pipe delimited file?

Hi guys, I need to know how i can ignore Pipe '|' if Pipe is coming as a column in Pipe delimited file for eg: file 1: xx|yy|"xyz|zzz"|zzz|12... using below awk command awk 'BEGIN {FS=OFS="|" } print $3 i would get xyz But i want as : xyz|zzz to consider as whole column... (13 Replies)
Discussion started by: rohit_shinez
13 Replies

4. Shell Programming and Scripting

Help with converting Pipe delimited file to Tab Delimited

I have a file which was pipe delimited, I need to make it tab delimited. I tried with sed but no use cat file | sed 's/|//t/g' The above command substituted "/t" not tab in the place of pipe. Sample file: abc|123|2012-01-30|2012-04-28|xyz have to convert to: abc 123... (6 Replies)
Discussion started by: karumudi7
6 Replies

5. UNIX for Advanced & Expert Users

Check Whether File is Pipe Delimited

Can anybody help me how to check whether a file is Pipe delimited or not? (1 Reply)
Discussion started by: Allwin333
1 Replies

6. UNIX for Dummies Questions & Answers

Reading a pipe delimited file

Hi Guys, i am reading a pipe delimited file using awk command. I have tested the gawk separately. it was fine. But when i execute the script. i am getting the following error saying command not found. Can somebody point out as what i am doing wrong. Cheers!!! (3 Replies)
Discussion started by: mac4rfree
3 Replies

7. Shell Programming and Scripting

How to convert a space delimited file into a pipe delimited file using shellscript?

Hi All, I have space delimited file similar to the one as shown below.. I need to convert it as a pipe delimited, the values inside the pipe delimited file should be as highlighted... AA ATIU2345098809 009697 005374 BB ATIU2345097809 005445 006518 CC ATIU9685098809 003215 003571 DD... (7 Replies)
Discussion started by: nithins007
7 Replies

8. Shell Programming and Scripting

Removal of carriage returns from a comma delimited file

Hi, I have a file which is having some carriage return in one of the field for which single line is coming in multiple lines. I want to combine all those multiple lines of that field into one line. Eg: Input: Id, Name, Location, Comments, Dept 2, John, US, I am from US. I... (5 Replies)
Discussion started by: mahish20
5 Replies

9. Shell Programming and Scripting

convert a pipe delimited file to a':" delimited file

i have a file whose data is like this:: osr_pe_assign|-120||4| osr_evt|-21||4| pe_avail|-21||4| osr_svt|-11||4| pe_mop|-13||4| instar_ready|-35||4| nsdnet_ready|-90||4|... (6 Replies)
Discussion started by: priyanka3006
6 Replies

10. Shell Programming and Scripting

How to split pipe delimited file

I have a pipe delimited input file as below. First byte of the each line indicate the record type. Then i need to split the file based on record_type = null,0,1,2,6 and create 5 files. How do i do this in a ksh script? Pls help |sl||SL|SL|SL|1996/04/03|1988/09/15|C|A|sl||||*|... (4 Replies)
Discussion started by: njgirl
4 Replies
Login or Register to Ask a Question