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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Insert a value in a pipe delimited line (unsig sed,awk)
# 1  
Old 07-02-2014
RedHat 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

Code:
a|b|c|d|1
a|b|c|d
a|b|c|d|e|1
a|b|c

so that output file looks like

Code:
a|b|x|c|d|1
a|b|x|c|d
a|b|x|c|d|e|1
a|b|x|c

I can do that using perl as below

Code:
#!/usr/bin/perl -w
use strict;

#inserting x at pos 3

while (<>) {
        my @arr=split('\|',$_);
        my $str=join('|',@arr[0..1],'x',@arr[2..$#arr]);
        print "$str";
}

However I need to do it in sed/awk but have no idea how Smilie
help!!!
# 2  
Old 07-02-2014
The RE can probably be condensed a bit but here is an awk version:

Code:
awk '{sub(/[^|]+\|[^|]+\|/,"&x|")}1'


...and using sed:

Code:
sed 's/\([^|]*|[^|]*|\)/\1x|/'


...or another awk way:

Code:
awk '{$2=$2"|x"}1' FS=\| OFS=\|


Last edited by pilnet101; 07-02-2014 at 08:39 AM..
# 3  
Old 07-02-2014
Code:
sed 's/|/|x|/2' file

EDIT: An alternative awk solution:
Code:
awk '{for (i=NF;i>2;i--) {$(i+1)=$i} $2="x"; print}' FS='|' OFS='|' file

But Scrutinizer's suggestion is a neater method of doing it via fields.

Last edited by CarloM; 07-02-2014 at 08:27 AM..
# 4  
Old 07-02-2014
You can also try:
Code:
awk '{$3=s OFS $3}1' s="x" FS=\| OFS=\| file

Or
Code:
sed 's/|/|x|/2' file

*edit* CarloM beat me to it with the sed suggestion..
# 5  
Old 07-02-2014
I knew there would be a shorter sed solution and you guys just confirmed it Smilie
# 6  
Old 07-02-2014
Wow!!!, thanks to all of you for your responses.
I got what I wanted Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to remove new line characters from data rows in a Pipe delimited file?

I have a file as below Emp1|FirstName|MiddleName|LastName|Address|Pincode|PhoneNumber 1234|FirstName1|MiddleName2|LastName3| Add1 || ADD2|123|000000000 2345|FirstName2|MiddleName3|LastName4| Add1 || ADD2| 234|000000000 OUTPUT : ... (1 Reply)
Discussion started by: styris
1 Replies

2. Shell Programming and Scripting

Insert empty columns inside a pipe delimited file

Hi All , I have pipe delimiter file with 11 columns . I need to insert 4 empty columns after column 10 . and After 11 column I need to insert a column which is having the same value for all the rows . My file 1|2|3|4|5|6|7|8|9|10|11 New file ... (11 Replies)
Discussion started by: Hypesslearner
11 Replies

3. Shell Programming and Scripting

sed and awk to insert a line after a para

hi I am having a file like this ############################## mod1 ( a(ll) , b( c), try(o) , oll(ll) go(oo) , al(ll) mm(al) , lpo(kka) kka(oop) ); mod2 ( jj(ll) , c( kk), try1q(o1) , ofll(lll) gao(oo1) , ala(llaa) mmf(adl) , lddpo(kkad) kkda(oodp) );... (20 Replies)
Discussion started by: kshitij
20 Replies

4. Shell Programming and Scripting

Use less pipe for grep or awk sed to print the line not include xx yy zz

cat file |grep -v "xx" | grep -v "yy" |grep -v "zz" (3 Replies)
Discussion started by: yanglei_fage
3 Replies

5. UNIX for Dummies Questions & Answers

find string and get the rest of the line in a pipe delimited file

Hi friends, I have a file where I should search for a string and get the rest of the line but without the delimiter using awk. for example I have the series of string in a file: input_string.txt bbb ccc aaa and the mapping file looks like this. mapping.txt aaa|12 bbb|23 ccc|43... (11 Replies)
Discussion started by: kokoro
11 Replies

6. Shell Programming and Scripting

how to Insert values in multiple lines(records) within a pipe delimited text file in specific cols

this is Korn shell unix. The scenario is I have a pipe delimited text file which needs to be customized. say for example,I have a pipe delimited text file with 15 columns(| delimited) and 200 rows. currently the 11th and 12th column has null values for all the records(there are other null columns... (4 Replies)
Discussion started by: vasan2815
4 Replies

7. Shell Programming and Scripting

How to insert a sequence number column inside a pipe delimited csv file using shell scripting?

Hi All, I need a shell script which could insert a sequence number column inside a dat file(pipe delimited). I have the dat file similar to the one as shown below.. |A|B|C||D|E |F|G|H||I|J |K|L|M||N|O |P|Q|R||S|T As shown above, the column 4 is currently blank and i need to insert sequence... (5 Replies)
Discussion started by: nithins007
5 Replies

8. Shell Programming and Scripting

sed returns different results while substitution on a pipe delimited file

Hi, 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 ... (3 Replies)
Discussion started by: vmenon
3 Replies

9. Shell Programming and Scripting

sed/awk to insert comment at defined line number

Hi there, may someone easily help me on this : I want to insert a text in a specific line number like : linenumb2start=`cat memory_map.dld | nl -ba | egrep -i "label" | cut -f1` line2insert=`expr $linenumb2start + 2` and now I need to replace something like {} with {comment} at... (8 Replies)
Discussion started by: homefp
8 Replies

10. UNIX for Advanced & Expert Users

insert pipe in the finish of the line

hi, i have a flat file, with lines (records), and fields, and each field is separated by pipe ( | ) : 1|078|012006|3,9 2|078|012006|8692275|4|2|GON3507090 2|078|012006|7655734|9|0|GON3507090 2|078|012006|7572405|5|4|GCR5N07090 what i need is to insert a pipe in the end of the line:... (3 Replies)
Discussion started by: DebianJ
3 Replies
Login or Register to Ask a Question