sed command to change 2nd field


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed command to change 2nd field
# 1  
Old 10-07-2006
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
dfd:*WW*ccc:12390:dfjis
cde:aaa:21311:dfjsid
then result is redirected to a new file.

Looking for dfd as pattern, then add *WW* to its second field by using sed.

I have tried to run below statement, which is working on the terminal but not in the script.
cat file | awk -F: '{print $2}' | sed 's:^:\*WW\*:'

I have read a couple of sed threads at this forum, but I am still not able to solve above question by myself. Pls help me out. Thanks.
# 2  
Old 10-07-2006
Code:
sed 's/^dfd:/&*WW*/' myFile.txt

# 3  
Old 10-07-2006
oh, excellent. Thanks a lot
# 4  
Old 10-08-2006
Quote:
Originally Posted by Julius
Hi

sqw:qqq:123124:uiqe
dfd:ccc:12390:dfjis
cde:aaa:21311:dfjsid

and, I want the output to be,
sqw:qqq:123124:uiqe
dfd:*WW*ccc:12390:dfjis
cde:aaa:21311:dfjsid
Hi again, I have one more question here. How can I use sed look the Kth field in the line and then replace it. eg. I want to look up 'cde' then replace the line
cde:aaa:21311:dfjsid
to
cde:aaa:00000:dfjsid
# 5  
Old 10-08-2006
in this case the only field you are manipulating is nueric so:

Code:
sed '/^cde/s/[0-9][0-9]*/00000/'

should work.

Having aid that, awk would be a much better tool in this situation:

Code:
awk 'BEGIN { FS=OFS=":" } /^cde/ { $3 = "00000" ; print }'

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed - use back reference in 2nd command

I have data that looks like this: <Country code="US"><tag>adsf</tag><tag>bdfs</tag></Country><Country code="CA"><tag>asdf</tag><tag>bsdf</tag></Country> I want to grab the country code save it, then drop each new "<..." onto a new line with the country code added to the beginning of each So,... (9 Replies)
Discussion started by: JenniferAmon
9 Replies

2. 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

3. Shell Programming and Scripting

Datestamp format 2nd change in csv file (awk or sed)

I have a csv file formatted like this: 2014-08-21 18:06:26,A,B,12345,123,C,1232,26/08/14 18:07and I'm trying to change it to MM/DD/YYYY HH:MM for both occurances. I have got this: awk -F, 'NR <=1 {print;next}{"date +%d/%m/%Y\" \"%H:%m -d\""$1 "\""| getline dte;$1=dte}1' OFS="," test.csvThis... (6 Replies)
Discussion started by: say170
6 Replies

4. 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

5. Shell Programming and Scripting

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: 1\23|\tmp\user mn\wer|\home\temp Expected output: 1\23|/tmp/user mn\wer|/home/temp I used sed 's/\\/\//g' ... (2 Replies)
Discussion started by: krishmaths
2 Replies

6. Shell Programming and Scripting

awk or sed? change field conditional on key match

Hi. I'd appreciate if I can get some direction in this issue to get me going. Datafile1: -About 4000 records, I have to update field#4 in selected records based on a match in the key field (Field#1). -Field #1 is the key field (servername) . # of Fields may vary # comment server1 bbb ccc... (2 Replies)
Discussion started by: RascalHoudi
2 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

where to specify the field name in SED command

HI Folks, I know the solution to my question is very easy but i could not find it out. There is CSV file with 20 fields with 5 rows contains different data as below: FBINV,LVO71,GAMFR1,D,104164,16677.36,20090625,102135,20090529,7267,KG,,,,0,,,,DTD,,O,O,,,,,,,,,300588136... (3 Replies)
Discussion started by: chandrakala
3 Replies

9. 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

10. Shell Programming and Scripting

how to specify the column(field) in sed command

the following command removes the first two characters of the first column, how can I change it to to remove the first two characters of the second column? sed 's/^..//' file1.txt > file2.txt Thanks Joseph (4 Replies)
Discussion started by: jdhahbi
4 Replies
Login or Register to Ask a Question