Find and delete part of field with awk or sed


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Find and delete part of field with awk or sed
# 1  
Old 01-31-2014
Find and delete part of field with awk or sed

I've got a file that looks like this (the whitespace between commas is intentional):

Code:
123456789,12,JOHN H DOE             ,DOE/JOHN H             ,,,DOE/JOHN H      ,,,,,123 FAKE STREET  ,SPRINGFIELD,XX,

I want to strip just the first name out of the third field so it reads "JOHN,". So far I have
Code:
awk -F ',' '{ print $3 }' FILENAM1 | awk '{ print $1 }' >FILENAM2

That strips the first name but also the rest of the file. I want the reverse: delete only what comes after the first name and keep everything else. Thanks in advance!
# 2  
Old 01-31-2014
I'm not sure if I understood your requirement. Can you also post what is the expected output for below input:
Code:
123456789,12,JOHN H DOE             ,DOE/JOHN H             ,,,DOE/JOHN H      ,,,,,123 FAKE STREET  ,SPRINGFIELD,XX,

# 3  
Old 01-31-2014
Sure. Sorry for the lack of clarity. Here is what I want the line to look like:
Code:
123456789,12,JOHN,DOE/JOHN H             ,,,DOE/JOHN H      ,,,,,123 FAKE STREET  ,SPRINGFIELD,XX,

Thanks!
# 4  
Old 01-31-2014
Code:
awk -F, '{sub(/ .*/,x,$3)}1' OFS=, file

This User Gave Thanks to Yoda For This Post:
# 5  
Old 01-31-2014
Thank you, Jedi Master. That worked perfectly. And now if you will permit a follow up question, how would I strip the next field so it keeps only the name up to the / ?.

I want my output to look like this:
Code:
123456789,12,JOHN,DOE,,,DOE/JOHN H      ,,,,,123 FAKE STREET  ,SPRINGFIELD,XX,

# 6  
Old 01-31-2014
Just use sub function on 4th field as well:
Code:
awk -F, '{sub(/ .*/,x,$3);sub(/\/.*/,x,$4)}1' OFS=, file

# 7  
Old 01-31-2014
Thanks again! I wasn't escaping the slash properly. And I didn't know I could string two awk functions together with a ; so you've properly schooled me today.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

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

2. Shell Programming and Scripting

awk to split one field and print the last two fields within the split part.

Hello; I have a file consists of 4 columns separated by tab. The problem is the third fields. Some of the them are very long but can be split by the vertical bar "|". Also some of them do not contain the string "UniProt", but I could ignore it at this moment, and sort the file afterwards. Here is... (5 Replies)
Discussion started by: yifangt
5 Replies

3. UNIX for Dummies Questions & Answers

sed or awk - removing part of line?

hi all, I am having trouble finding the right string for this - I dont know whether to use awk or sed.. If I have a file with alot of names and phone numbers like this McGowan,Sean 978-934-4000 Kilcoyne,Kathleen 603-555-1212 Club603,The 617-505-1332 Boyle,William 301-444-1221 And... (11 Replies)
Discussion started by: alis
11 Replies

4. Shell Programming and Scripting

FS did not show after delete one field in awk

Hello, I met a problem to delete one column out of 32, say column 13. I tried awk and gawk but neither worked. The problem is col13 can deleted but the field separator is gone too, so that the output is not in tab-delimited format. Or, col13 can't be deleted. I want the output file with TAB as... (3 Replies)
Discussion started by: yifangt
3 Replies

5. Shell Programming and Scripting

Replace part of a line with sed/awk

Hello I have a document and in this document I have several occurrence of "VAR == xxxxxxx" and xxxxx can be anything. I don't know what it is. I want to replace the 'xxxxx's with something I know. What I know however, is the line numbers of the VAR =='s in the file. How can I replace... (1 Reply)
Discussion started by: alirezan
1 Replies

6. UNIX for Advanced & Expert Users

Urgent Help required : awk/sed help to find pattern and delete till end of line

Hi, I need help with using an awk or sed filter on the below line ALTER TABLE "ACCOUNT" ADD CONSTRAINT "ACCOUNT_PK" PRIMARY KEY ("ACCT_ID") USING INDEX PCTFREE 10 INITRANS 2 MAXTRANS 255 STORAGE(INITIAL 65536 FREELISTS 1 FREELIST GROUPS 1) TABLESPACE "WMC_DATA" LOGGING ENABLE Look for... (1 Reply)
Discussion started by: rajan_san
1 Replies

7. Shell Programming and Scripting

Urgent! Sed/Awk Filter Find Pattern Delete Till End Of Line

Hi, I need help with using an awk or sed filter on the below line ALTER TABLE "ACCOUNT" ADD CONSTRAINT "ACCOUNT_PK" PRIMARY KEY ("ACCT_ID") USING INDEX PCTFREE 10 INITRANS 2 MAXTRANS 255 STORAGE(INITIAL 65536 FREELISTS 1 FREELIST GROUPS 1) TABLESPACE "WMC_DATA" LOGGING ENABLE Look for... (2 Replies)
Discussion started by: rajan_san
2 Replies

8. Shell Programming and Scripting

sed: find match and delete the line above

I am searching a dhcpd.conf to find the hardware ethernet match, then once the match is found delete just the line above it. For example: testmachine.example { hardware ethernet 00:00:00:00:00:00; fixed address 192.168.1.100; next-server 192.168.1.101; filename "linux-install/pxelinux.0"; }... (3 Replies)
Discussion started by: cstovall
3 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