sed to work on 2nd field only


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed to work on 2nd field only
# 1  
Old 04-01-2013
sed to work on 2nd field only

I have a requirement to replace "\" with "/" in only the 2nd field of the input file which has 2 fields. The field delimiter is "|"

Sample records from input file:
Code:
1\23|\tmp\user
mn\wer|\home\temp

Expected output:
Code:
1\23|/tmp/user
mn\wer|/home/temp

I used
Code:
sed 's/\\/\//g'

This would replace all "\" to "/". I want the replacement to happen only in the second field.

Any suggestions?
# 2  
Old 04-01-2013
Does it have to be sed? awk would be very simple:
Code:
$ awk -F\| '{gsub(/\\/, "/",$2)}1' OFS=\| file
1\23|/tmp/user
mn\wer|/home/temp


With sed, try
Code:
$ sed 'h;s:.*|::;s:\\:/:g;x;s:|.*:|:;G;s:\n::' file
1\23|/tmp/user
mn\wer|/home/temp


Last edited by RudiC; 04-01-2013 at 07:47 AM..
This User Gave Thanks to RudiC For This Post:
# 3  
Old 04-01-2013
Thanks, it worked although the second option is a little tougher to understand!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Combine Similar Output from the 2nd field w.r.t 1st Field

Hi, For example: I have: HostA,XYZ HostB,XYZ HostC,ABC I would like the output to be: HostA,HostB: XYZ HostC:ABC How can I achieve this? So far what I though of is: (1 Reply)
Discussion started by: alvinoo
1 Replies

2. Shell Programming and Scripting

Awk,sed : change every 2nd field ":" to "|"

Hi Experts, I have a string with colon delimited, want 2nd colon to be changed to a pipe. data: 101:8:43:4:72:14:41:69:85:3:137:4:3:0:4:0:9:3:0:3:12:3: I am trying with sed, but can change only 1 occurance: echo "101:8:43:4:72:14:41:69:85:3:137:4:3:0:4:0:9:3:0:3:12:3:" | sed 's/:/|/2'... (5 Replies)
Discussion started by: rveri
5 Replies

3. AIX

2nd SSH doesn't work with AD

Recently I decided to intall second daemon of SSH for Winbind users. I mean I have configuration AIX + Samba + AD and I can login to the server via SSH with AD accounts to 22 port without any problems. But now I have second installation of OpenSSH and don't understand why I can't do the same... (6 Replies)
Discussion started by: jess_t03
6 Replies

4. Shell Programming and Scripting

sed to replace a field from a line with another field

i have something like this, cat filename.txt hui this si s"dfgdfg" omeone ipaddress="10.19.123.104" wel hope this works i want to replace only 10.19.123.104 with different ip say 10.19.123.103 i tried this sed -i "s/'ipaddress'/'ipaddress=10.19.123.103'/g" filename.txt ... (1 Reply)
Discussion started by: vivek d r
1 Replies

5. Shell Programming and Scripting

Append 1st field from a file into 2nd field of another file

Hi, I've internally searched through forums for about 2+ hours. Unfortunately, with no luck. Although I've found some cases close to mine below, but didn't help so much. Actually, I'm in short with time. So I had to post my case. Hoping that you can help. I have 2 files, FILE1 ... (1 Reply)
Discussion started by: amurib
1 Replies

6. Shell Programming and Scripting

Appending 1st field in a file into 2nd field in another file

Hi, I've internally searched through forums for about 2+ hours. Unfortunately, with no luck. Although I've found some cases close to mine below, but didn't help so much. Actually, I'm in short with time. So I had to post my case. Hoping that you can help. I have 2 files, FILE1 ... (0 Replies)
Discussion started by: amurib
0 Replies

7. Shell Programming and Scripting

Remove leading zeroes in 2nd field using sed

Hi Forum. I tried searching the forum but couldn't find a solution for my question. I have the following data and would like to have a sed syntax to remove the leading zeroes from the 2nd field only: Before: 2010-01-01|123|1|1000|2000|500|1500|600|700... (18 Replies)
Discussion started by: pchang
18 Replies

8. Shell Programming and Scripting

Sort alpha on 1st field, numerical on 2nd field (sci notation)

I want to sort alphabetically on the first field and sort in descending numerical order on the 2nd field. With a normal "sort -r -n" it does this: abc ||| 5e-05 ||| bla abc ||| 3 ||| ble def ||| 1 ||| abc def ||| 0.2 ||| def As you can see it ignores the fact that 5e-05 is actually 0.00005... (1 Reply)
Discussion started by: FrancoisCN
1 Replies

9. Shell Programming and Scripting

get a name according to the 2nd field

Hi, I have a file with this format: name number -------------- ----------- AAA 11700 BBB 6000 CCC 6000 I want to catch any of these names if number > 200 How to do that? at a... (4 Replies)
Discussion started by: melanie_pfefer
4 Replies

10. Shell Programming and Scripting

sed command to change 2nd field

Hi I am a beginner to sed command, here I have a question about using sed to add a few characters into a token of a string. For example, I have a file, sqw:qqq:123124:uiqe dfd:ccc:12390:dfjis cde:aaa:21311:dfjsid and, I want the output to be, sqw:qqq:123124:uiqe... (4 Replies)
Discussion started by: Julius
4 Replies
Login or Register to Ask a Question