where to specify the field name in SED command


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting where to specify the field name in SED command
# 1  
Old 07-03-2009
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,,,,,,,,,30058 8136
FBINV,LVO71,GAMFR1,D,104164,16677.36,20090625,102135,20090529,7267,KG,,,,0,,,,DTD,,O,O,,,,,,,,,30058 8136
FBINV,LVO71,GAMFR1,D,104164,16677.36,20090625,102135,20090529,7267,KG,,,,0,,,,DTD,,O,O,,,,,,,,,30058 8136
FBINV,LVO71,GAMFR1,D,104164,16677.36,20090625,102135,20090529,7267,KG,,,,0,,,,DTD,,O,O,,,,,,,,,30058 8136
FBINV,LVO71,GAMFR1,D,104164,16677.36,20090625,102135,20090529,7267,KG,,,,0,,,,DTD,,O,O,,,,,,,,,30058 8136


I want to replace KG with K in field 11th in all the rows.

Can you please help me with a code to replace it in all the lines.processing line by line.There can be any number of rows in a file.


thanks a lot
# 2  
Old 07-03-2009
Try this.

awk 'BEGIN { FS=OFS="," } /KG/ { $11 = "K" ; print }' filename
# 3  
Old 07-03-2009
sed 's/,KG,/,K,/g' <input-file>
# 4  
Old 07-04-2009
Code:
 
I want to replace KG with K in field 11th in all the rows.

None of the solutions from jayan_jay or lathavim are checking for the 11th filed.

Bit change to lathamvim's solution :

Code:
awk 'BEGIN {FS=OFS=","} $11 ~/KG/ { $11 = "K" ; print }'  File_name.txt


Last edited by panyam; 07-04-2009 at 09:13 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Command/script to match a field and print the next field of each line in a file.

Hello, I have a text file in the below format: Source Destination State Lag Status CQA02W2K12pl:D:\CAQA ... (10 Replies)
Discussion started by: pocodot
10 Replies

2. Shell Programming and Scripting

sed : Second field not getting displayed in the output

Hi , I have a file (input.txt) with the below contents : 20150223T12:00:25 hostnamex 20150716T10:40:54 hostnamey 20150202T20:08:03 hostnamez I want the output to be like below i.e excluding the timestamp ( THH:MM:SS) in the above file as follows : 20150223 hostnamex 20150716... (2 Replies)
Discussion started by: rahul2662
2 Replies

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

4. Shell Programming and Scripting

sed command to remove the first field from a '|' delimited file

Hi I have a file with fields delimited by |. I need to remove the first field from the file. I tried cut but it just extracts that field. sample.output abc|100|name1 cde|200|name2 efg|300|name3 Output should be sample.output 100|name1 200|name2 300|name3 thanks Var (6 Replies)
Discussion started by: var285
6 Replies

5. Shell Programming and Scripting

Trying to use sed to remove the value of one field from another field

I'm trying to use sed to remove the value of one field from another field. For example: cat inputfile 123|ABC|Generic_Textjoe@yahoo.com|joe@yahoo.com|DEF 456|GHI|Other_recordjohn@msn.com|john@msn.com|JKL 789|MNO|No_Email_On_This_One|smith@gmail.com|PQR I would like to remove the email... (2 Replies)
Discussion started by: bribri87
2 Replies

6. Shell Programming and Scripting

Loop with sed command to replace line with sed command in it

Okay, title is kind of confusion, but basically, I have a lot of scripts on a server that I need to replace a ps command, however, the new ps command I'm trying to replace the current one with pipes to sed at one point. So now I am attempting to create another script that replaces that line. ... (1 Reply)
Discussion started by: cbo0485
1 Replies

7. Shell Programming and Scripting

Conditional edit for a field using sed

Hi I want to repalce a field in a txt file on solaris with say 100 records and each record having a total of 10 fields separated by a ~ . based on the following condition the record should be edited or else the record should be written as it is to a if the seventh field is 'XX' and if... (2 Replies)
Discussion started by: acharania2011
2 Replies

8. Linux

awk/sed for splitting a field into two

I have a tab delimitted dataset with 4 fields. I like to split the second field into two, and have 5 fields. I like to remove the "-" sign when I get a new fiel. would you help? It is like: 1223 100-5 rr dd I need it like: 1223 100 5 rr dd (2 Replies)
Discussion started by: sire
2 Replies

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

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