Remove part of a column in a text file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Remove part of a column in a text file
# 1  
Old 01-15-2010
Remove part of a column in a text file

I have a text file with delimiter "|" and sometimes the zipcode is in "5th" column or "6th" column. I want to scan the file and remove the "-2323" from the zipcode which has zip+4 digits

From
Code:
blah|blah|foo|bar||blah|945523-232|USA
 blah|blah|foo|bar||blah|foo|94555-2323|USA

To
Code:
blah|blah|foo|bar||blah|945523|USA
blah|blah|foo|bar||blah|foo|94555|USA

# 2  
Old 01-15-2010
the samples you gave have ZIPs in the 7th and the 8-th columns (and 5th and 6th):
Code:
nawk -F'|' -v OFS='|' '{$7=substr($7,1, index($7,"-")-1)}1' myFile

# 3  
Old 01-15-2010
Or with sed:
Code:
sed 's/-.*|/|/' file

# 4  
Old 01-15-2010
Tools Can clean up the awk/gawk, but...

Code:
>cat zips.txt | gawk  -F"|" '{FS=IFS=OFS="|"; $7=substr($7,1,5); $8=substr($8,1,5); print $0}'
blah|blah|foo|bar||blah|94552|USA
blah|blah|foo|bar||blah|foo|94555|USA

# 5  
Old 01-15-2010
The "sed 's/-.*|/|/' file" works for me however how can I specify to remove the "-" on a particular column so that it doesnt touch ant other fields with "-" ? I n this case I only want to touch the field 12

blah|08-232|foo|bar|blah|aaa|aaa|asa|as|as|foo|94555-7777|blah
# 6  
Old 01-15-2010
Assuming the zip code has a fixed length of 5 digits you can try this:
Code:
sed 's/\(.*[0-9][0-9][0-9][0-9][0-9]\)-[^|]*\(|.*\)/\1\2/' file

# 7  
Old 01-15-2010
Quote:
Originally Posted by Franklin52
Assuming the zip code has a fixed length of 5 digits you can try this:
Code:
sed 's/\(.*[0-9][0-9][0-9][0-9][0-9]\)-[^|]*\(|.*\)/\1\2/' file

Quote:
Exactly 5 digit zip code
sed 's/\(.*[0-9]\{5\}\)-[^|]*\(|.*\)/\1\2/' file

More than 5 digit zip code
sed 's/\(.*[0-9]\{5,\}\)-[^|]*\(|.*\)/\1\2/' file
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Remove part of the file using a condition

Gents, Is there the chance to remove part of the file, Taking in consideration this condition. For each record the first row start with the string % VE should be 56 rows for each records.. first row = % VE last row = % sw total 56 rows for each record. Then in the case that the... (4 Replies)
Discussion started by: jiam912
4 Replies

2. Shell Programming and Scripting

How to remove duplicated column in a text file?

Dear all, How can I remove duplicated column in a text file? Input: LG10_PM_map_19_LEnd 1000560 G AA AA AA AA AA GG LG10_PM_map_19_LEnd 1005621 G GG GG GG AA AA GG LG10_PM_map_19_LEnd 1011214 A AA AA AA AA GG GG LG10_PM_map_19_LEnd 1011673 T TT TT TT TT CC CC... (1 Reply)
Discussion started by: huiyee1
1 Replies

3. Shell Programming and Scripting

Remove the values from a certain column without deleting the Column name in a .CSV file

(14 Replies)
Discussion started by: dhruuv369
14 Replies

4. Shell Programming and Scripting

Use grep/awk to remove part of column

hi all, how can i use grep or awk to clean the following input data: n<>the<>96427210 861521305 123257583 n<>obj<>79634223 861521305 79634223 n<>nmod<>68404733 861521305 68422718 where the desired results is to remove all non-numeric characters?: 96427210 861521305 123257583 ... (5 Replies)
Discussion started by: owwow14
5 Replies

5. Shell Programming and Scripting

How to remove the date part of the file?

Hi Gurus, I have file name like: abcd.20131005.dat I need to remove mid part of data final name should be abcd.dat thanks in advance (2 Replies)
Discussion started by: ken6503
2 Replies

6. Shell Programming and Scripting

Awk: Need help replacing a specific column in a file by part of a column in another file

Hi, I have two input files as File1 : ABC:client1:project1 XYZ:client2-aa:project2 DEF:client4:proj File2 : client1:W-170:xx client2-aa:WT-04:yy client4:L-005A:zz Also, array of valid values can be hardcoded like Output : ABC:W:project1 XYZ:WT:project2 (1 Reply)
Discussion started by: aa2601
1 Replies

7. Shell Programming and Scripting

Remove part of the file

I have an xml file, from where I need to take out Application2 entries and keep the others. I need to remove from <product> to </product> and the key element to look for while removing should be <application> as other pairs can be same for others. <product> ... (10 Replies)
Discussion started by: chiru_h
10 Replies

8. Shell Programming and Scripting

remove certain part of file name

Hi, Is it possible to remove the first part of the file name using find. i.e i have something like 2006abc.txt , 1007bed.txt etc, I wanna rename them to abc.txt , bed.txt I tried some stupid way.. find . -name '*.txt' -exec mv {} `cut -f2-5 -d"_" {}` \; somehow iam not getting it. ... (3 Replies)
Discussion started by: braindrain
3 Replies

9. HP-UX

How do I take out(remove) the date part in the file name?

Hi Guys here I am again, I have two files in a specified location. Location ex: /opt/xdm/input/ input file names: 1. abc_app.yyyymmdd.dtd 2. abd_app.yyyymmdd.dtd I need to build a code that reads the files from the location based on the oldest date and cuts the date part... (5 Replies)
Discussion started by: ruthless
5 Replies
Login or Register to Ask a Question