On Match Delete Field


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users On Match Delete Field
# 1  
Old 03-07-2011
Tools On Match Delete Field

Hi,

A text file contains lines of fields seperated by some delimiter; space for example; some lines but not all will have in the second field a value like an inital (e.g., A. or B. or Jr.) for those lines with such a value in field 2, we wish to have that value deleted. We also wish to do this as a one-liner. Here's what we have so far, but this omits all the other lines without a period in any field. What say you?

perl -i.bak -ane "print unless $F[2] =~ m{ \A (?: [[:alpha:]] | Jr) \. \z }xms" in.file


The following lines illustrate the issue:
Thomas, A. Alexandria S. Perl Programmer Las Vegas NV.
Williams, Jr., Michael A. C Programmer New York N.Y.
Silver, Susan Java Programmer San Jose CA
Altips, Alvin C. Tcl Programmer Chicago IL.

The challenge is to remove the A. and Jr., from the first two records, leaving the remaining fields in all lines with periods intact, and moreover leaving all other lines in the in.file. Thank you.
# 2  
Old 03-08-2011
Well, you need a definite spec of when field 2 is to be removed, as Susan is just a longer second field. Perhaps we can key on the '.' trailing dot/stop. I am a sed guy, not su much PERL and awk:
Code:
 
sed 's/^\([^ ]* \)[^ .]*\. /\1/'

Narrative: sed reads stdin, and if there is a dot at the end of the second space separated field, removes it by replacing the first two fields plus separaors with the first field plus separator, writes all to stdout.
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 match field between two files and use conditions on match

I am trying to look for $2 of file1 (skipping the header) in $2 of file2 (skipping the header) and if they match and the value in $10 is > 30 and $11 is > 49, then print the line from file1 to a output file. If no match is foung the line is not printed. Both the input and output are tab-delimited.... (3 Replies)
Discussion started by: cmccabe
3 Replies

2. Shell Programming and Scripting

awk to match value to a field within +/- value

In the awk below I use $2 of filet to search filea for a match. If the values in $2 are exact match this works great. However, that is not always the case, so I need to perform the search using a range of + or - 2. That is if the value in filea $2 is within + or - 2 of filet $2 then it is matched.... (6 Replies)
Discussion started by: cmccabe
6 Replies

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

4. Shell Programming and Scripting

Match and Grep the nearest value in last field

Gents I have this input file file1 (uniq records) 54503207851 170211240 54503207911 170210837 54503208111 170215105 54503208112 170215210 54655210011 170223140 54655210091 170223738 54655210172 170224355 54655210251 170224741 54655210331 170225039 54655210411 170225505 54655210492... (13 Replies)
Discussion started by: jiam912
13 Replies

5. Shell Programming and Scripting

Match complete field

hello, i have a little problem, i want match the complete field ($1 or $2) with a complete word in another variable. example: i have a file with either one or two words per lane: hsa-mir-4449 hsa-mir-4707 hsa-mir-4707* hsa-mir-4707 novelMiR_3551 novelMiR_3563 novelMiR_4330... (4 Replies)
Discussion started by: ace13
4 Replies

6. UNIX for Dummies Questions & Answers

Match pattern in a field, print pattern only instead of the entire field

Hi ! I have a tab-delimited file, file.tab: Column1 Column2 Column3 aaaaaaaaaa bbtomatoesbbbbbb cccccccccc ddddddddd eeeeappleseeeeeeeee ffffffffffffff ggggggggg hhhhhhtomatoeshhh iiiiiiiiiiiiiiii ... (18 Replies)
Discussion started by: lucasvs
18 Replies

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

8. Shell Programming and Scripting

first field match

Hi All, Thanks for your help in advance I am parsing a log file where the first field of each line can have the same key value but not more than 3 times in a row. Varying value of that first field changes as you go through the log but it either appears 3 times or two and sometimes only once.... (1 Reply)
Discussion started by: dragrid
1 Replies

9. Shell Programming and Scripting

how do i pattern match a field with awk?

hi, let's say $numbers = "324 350 587" an so on... what i'm trying to do is this: awk -v numbers="$numbers" '{if (numbers ~ /$2/) print $0, "bla bla"}' file # file looks like this: 214 ..... 215 ... 216 .... 250 ... 324 325 ... 350 something ... ... 587 ... (4 Replies)
Discussion started by: someone123
4 Replies

10. Shell Programming and Scripting

match field between 2 files

I would like to do the following in bash shell. file a a:1 b:2 c:3 file b a:work:apple b:baby:banana c:candy:cat d:desk:dog I would like to match field 1 in file a to file b, if there's a match I would like to append field 2 in file a to field 3 in file b. Thank you. (8 Replies)
Discussion started by: phamp008
8 Replies
Login or Register to Ask a Question