Remove part of a string from second field


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Remove part of a string from second field
# 1  
Old 09-20-2011
Error Remove part of a string from second field

I have a file in below format (pipe delimited):
Code:
1234__abc|John__abc|xyz
3345__abc|Kate__abc|xyz
55344|Linda__abc|xyz
33434|Murray|xyz

I want to remove any occurence of "__abc" in the second field of this file.
I did some research and found a way to replace the entire second field with another string:
Code:
sed 's/^\([^|]*\)|[^|]*|/\1|9999|/'

But I am not able to remove the "__abc" alone in the second field. Any help to do this would me much appreciated.

Last edited by Scott; 09-20-2011 at 08:02 AM.. Reason: Added code tags
# 2  
Old 09-20-2011
Code:
$ cat fil
1234__abc|John__abc|xyz
3345__abc|Kate__abc|xyz
55344|Linda__abc|xyz
33434|Murray|xyz

Output:
Code:
$ awk -F"|" '{sub("__abc","",$2);}1' OFS="|" fil
1234__abc|John|xyz
3345__abc|Kate|xyz
55344|Linda|xyz
33434|Murray|xyz

Guru.
This User Gave Thanks to guruprasadpr For This Post:
# 3  
Old 09-20-2011
Thanks a lot. That worked.
# 4  
Old 09-20-2011
well if you have already touched sed, you were very close.

Code:
sed 's/__abc//2'  file

will give you what you need. I guess the missing part was the "2", right?
# 5  
Old 09-20-2011
@sk1418 :

No, your statement would remove the second "__abc" found.
So you would miss the __abc occurrence that appear in the third line because it is the first occurrence in the line.
Code:
$ cat tst
1234__abc|John__abc|xyz
3345__abc|Kate__abc|xyz
55344|Linda__abc|xyz
33434|Murray|xyz

Code:
$ sed 's/__abc//2' tst
1234__abc|John|xyz
3345__abc|Kate|xyz
55344|Linda__abc|xyz
33434|Murray|xyz

But you could go with this instead (assuming the whole file having the same formatting than the given example) :
Code:
$ cat tst
1234__abc|John__abc|xyz
3345__abc|Kate__abc|xyz
55344|Linda__abc|xyz
33434|Murray|xyz

Code:
$ sed 's/|\(.*\)__abc|/|\1|/' tst
1234__abc|John|xyz
3345__abc|Kate|xyz
55344|Linda|xyz
33434|Murray|xyz


Last edited by ctsgnb; 09-20-2011 at 09:50 AM..
# 6  
Old 09-20-2011
Quote:
Originally Posted by ctsgnb
@sk1418 :

No, your statement would remove the second "__abc" found.
So you would miss the __abc occurrence that appear in the third line because it is the first occurrence in the line.
Code:
$ cat tst
1234__abc|John__abc|xyz
3345__abc|Kate__abc|xyz
55344|Linda__abc|xyz
33434|Murray|xyz

Code:
$ sed 's/__abc//2' tst
1234__abc|John|xyz
3345__abc|Kate|xyz
55344|Linda__abc|xyz
33434|Murray|xyz

But you could go with this instead (assuming the whole file having the same formatting than the given example) :
Code:
$ cat tst
1234__abc|John__abc|xyz
3345__abc|Kate__abc|xyz
55344|Linda__abc|xyz
33434|Murray|xyz

Code:
$ sed 's/|\(.*\)__abc|/|\1|/' tst
1234__abc|John|xyz
3345__abc|Kate|xyz
55344|Linda|xyz
33434|Murray|xyz


thanks for pointing this out. I didn't notice the special 3rd line. Smilie

your sed 's/|\(.*\)__abc|/|\1|/' tst works great for this example. however also not so generic.

e.g.
Code:
kent$  cat a
1234__abc|John__abc|xyz
3345__abc|Kate__abc|xyz
55344|Linda__abc|xyz__abc|xx
33434|Murray|xyz

yours:
kent$  sed 's/|\(.*\)__abc|/|\1|/' a
1234__abc|John|xyz
3345__abc|Kate|xyz
55344|Linda__abc|xyz|xx
33434|Murray|xyz

I made one with sed, it works, however don't know if it is the best solution with sed.

Code:
kent$  sed -r 's/\|/\x034/2;s/__abc\x034/|/;s/\x034/|/' a
1234__abc|John|xyz
3345__abc|Kate|xyz
55344|Linda|xyz__abc|xx
33434|Murray|xyz

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Print particular string in a field of csv file - part 2

Hi, all I need your help and suggestions. I want to print particular strings in a field of a csv file and show them in terminal. Here is an example of the csv file. SourceFile,Airspeed,GPSLatitude,GPSLongitude,Temperature,Pressure,Altitude,Roll,Pitch,Yaw... (7 Replies)
Discussion started by: refrain
7 Replies

2. Linux

How do I format a Date field of a .CSV file with multiple commas in a string field?

I have a .CSV file (file.csv) whose data are all enclosed in double quotes. Sample format of the file is as below: column1,column2,column3,column4,column5,column6, column7, Column8, Column9, Column10 "12","B000QRIGJ4","4432","string with quotes, and with a comma, and colon: in... (3 Replies)
Discussion started by: dhruuv369
3 Replies

3. Shell Programming and Scripting

[Solved] sort on numeric part of field

I have ran into a heavy case of PEBCAK*) and could need some advice on what i do wrong: OS is Linux (kernel 2.6.35), sort --version reports "8.5" from 2010, shell is ksh. Originally i had a file with with the following structure: hdisk1 yyy hdisk2 yyy hdisk3 yyy hdisk4 yyy hdisk5 yyy... (2 Replies)
Discussion started by: bakunin
2 Replies

4. Shell Programming and Scripting

awk and get a part of field ?

I have a list of log.txt, thisis a flatfile separate by a pipe | 2012/06/23 18:58:15 | 4:sabercats pid=020272 opened Boards 0, 1, 2, 3 for /home/directory_germany/germany/location/qt/NET12/full_111_ddr5_soq523_2X_FV_4BD_PD3_0.qt/dbFiles/germany.proto |berlin|test1|test2 2012/06/25 17:40:56... (2 Replies)
Discussion started by: sabercats
2 Replies

5. Shell Programming and Scripting

Awk Search text string in field, not all in field.

Hello, I am using awk to match text in a tab separated field and am able to do so when matching the exact word. My problem is that I would like to match any sequence of text in the tab-separated field without having to match it all. Any help will be appreciated. Please see the code below. awk... (3 Replies)
Discussion started by: rocket_dog
3 Replies

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

7. Shell Programming and Scripting

How to select or make reference to, part of a field

For a field format such as AAL1001_MD82, how do I select(and use in if statement) only the last four elements( in this case MD82) or the first three elements (in this case AAL)? For instance, how do I do the following - if first three elements of $x == yyy, then ... (5 Replies)
Discussion started by: akshaykr2
5 Replies

8. Shell Programming and Scripting

remove last field from string

Hi everybody! I want to cut the last field from string, can anybody help me?? String input:: /abc1/abc2/abc3 output:: /abc1/abc2 Regards, Kiran (2 Replies)
Discussion started by: dddkiran
2 Replies

9. Shell Programming and Scripting

Extract Part of string from 3rd field $3 using AWK

I'm executing "wc -lc" command in a c shell script to get record count and byte counts and writing them to a file. I get the result with the full pathname of the file. But I do not want the path name to be printed in the output file. I heard that using Awk we can get this but I don't have any... (4 Replies)
Discussion started by: stakuri
4 Replies

10. Shell Programming and Scripting

Moving Part of a field to another field using AWK

Hi there, I have a comma seperated file with nine fields the fields are rerate: "numberTX",field2,field3,field4,field5..... I want to do this to the file reate: "field5TX",field2,field3,field4,field5 I know I can do this using AWK, but the thing giving me fits is that I... (5 Replies)
Discussion started by: rjsha1
5 Replies
Login or Register to Ask a Question