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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Trying to use sed to remove the value of one field from another field
# 1  
Old 12-22-2010
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:

Code:
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 address that is in the 4th field, from the text that is in the 3rd field, if it exists there (in the 3rd record there is nothing to remove).

The goal is for the output file to look like this:
Code:
123|ABC|Generic_Text|joe@yahoo.com|DEF
456|GHI|Other_record|john@msn.com|JKL
789|MNO|No_Email_On_This_One|smith@gmail.com|PQR


I thought a sed command like the following would do the trick:
Code:
sed 's/\([^|]*|[^|]*|\)\([^|]*\\3|\)\([^|]*\)\(.*\) /\1\2\3\4/g' inputfile

but it's not removing anything from the 3rd field.

Any ideas how to accomplish this task?
Thanks!


Moderator's Comments:
Mod Comment Please use code tags when posting data and code samples!

Last edited by Franklin52; 12-23-2010 at 04:05 AM..
# 2  
Old 12-23-2010
you could simply go for a awk solution..Guess it's quite complex with sed to this kind of texts(anyway let's try Smilie)
Code:
awk -F"|" '{sub($4,"",$3); OFS=FS}1' inputfile > outfile


Last edited by michaelrozar17; 12-23-2010 at 02:55 AM.. Reason: eloborated
This User Gave Thanks to michaelrozar17 For This Post:
# 3  
Old 12-23-2010
Code:
sed 's/\(.*\)\(.*\)|\(\2|.*\)/\1|\3/' inputFile

Above awk looks simpler though.
This User Gave Thanks to anurag.singh For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to adjust coordinates in field based on sequential numbers in another field

I am trying to output a tab-delimited result that uses the data from a tab-delimited file to combine and subtract specific lines. If $4 matches in each line then the first matching sequential $6 value is added to $2, unless the value is 1, then the original $2 is used (like in the case of line... (3 Replies)
Discussion started by: cmccabe
3 Replies

2. Shell Programming and Scripting

Display combination of 4 field uniqe record and along with concatenate 5th and 6th field.

Table ACN|NAME|CITY|CTY|NO1|NO2 115|AKKK|ASH|IND|10|15 115|AKKK|ASH|IND|20|20 115|AKKK|ASH|IND|30|35 115|AKKK|ASH|IND|30|35 112|ABC|FL|USA|15|15 112|ABC|FL|USA|25|20 112|ABC|FL|USA|25|45 i have written shell script using cut command and awk programming getting error correct it and add... (5 Replies)
Discussion started by: udhal
5 Replies

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

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

AWK: Pattern match between 2 files, then compare a field in file1 as > or < field in file2

First, thanks for the help in previous posts... couldn't have gotten where I am now without it! So here is what I have, I use AWK to match $1 and $2 as 1 string in file1 to $1 and $2 as 1 string in file2. Now I'm wondering if I can extend this AWK command to incorporate the following: If $1... (4 Replies)
Discussion started by: right_coaster
4 Replies

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

7. Shell Programming and Scripting

awk, comma as field separator and text inside double quotes as a field.

Hi, all I need to get fields in a line that are separated by commas, some of the fields are enclosed with double quotes, and they are supposed to be treated as a single field even if there are commas inside the quotes. sample input: for this line, 5 fields are supposed to be extracted, they... (8 Replies)
Discussion started by: kevintse
8 Replies

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

9. Shell Programming and Scripting

Using sed to remove lines where field is empty

I was just looking at this post: https://www.unix.com/shell-programming-scripting/22893-delete-multiple-empty-lines.html. and I am looking to achieve the same with sed. So the idea is to delete lines from a file where a certain field has no value. Inputfile: EMID MMDDYY HOURS JOB EMNAME 0241... (4 Replies)
Discussion started by: figaro
4 Replies

10. 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
Login or Register to Ask a Question