Prepend text, different matched 1st letters


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Prepend text, different matched 1st letters
# 1  
Old 08-01-2016
Prepend text, different matched 1st letters

Prepending lines with: your, the, a or an based on 1st letter match. You'll see my problem below:
Code:
sed '/^p\|^f\|^c\|^d\|^l/ s/^/your /' list.txt > your.txt && sed  '/^v\|^j\|^k\|^m\|^n\|^s/ s/^/the /' your.txt > the.txt && sed '/^b\|^g\|^h\|^q\|^r\|^t\|^w\|^z/ s/^/a /' the.txt > a.txt  && sed '/^a\|^e\|^i\|^o\|^u/ s/^/an /' a.txt > an.txt

Example of bad overlapping output:
Code:
an a reinforced hose
an a the non-reinforced hose

Thanks for help.
# 2  
Old 08-01-2016
Code:
awk '
/^[pfcdl]/ {$0="your " $0; print ; next}
/^[vjkmns]/ {$0="the " $0; print ; next}
/^[bghqrtwz]/ {$0="a " $0; print ; next}
/^[aeiou]/ {$0="an " $0; print ; next}
1' list.txt

This User Gave Thanks to rdrtx1 For This Post:
# 3  
Old 08-01-2016
You need to make sure that once a modification is done, it won't be subject to another one (e.g. by adapting the sequence of modifications). Try
Code:
sed 's/^[aeiou]/an &/; s/^[bghqrtwz]/a &/; s/^[pfcdl]/your &/; s/^[vjkmns]/the &/ ' file
a reinforced hose
the non-reinforced hose

This User Gave Thanks to RudiC For This Post:
# 4  
Old 08-01-2016
Note also that if you're trying to use "a" or "an" before an English word, you need more than just the first letter to make the decision: a hose is correct and an hose is wrong, but an hour is correct and a hour is wrong.
# 5  
Old 08-01-2016
Hello p1ne,

rdrtx1's solution could be little modified(where removing the string concatination to line and directly printing it rather) to as follows may help you in same too.
Code:
awk '
/^[pfcdl]/ {print "your " $0 ; next}
/^[vjkmns]/ {print "the " $0 ; next}
/^[bghqrtwz]/ {print "a " $0 ;  next}
/^[aeiou]/ {print "an " $0   ; next}
1' list.txt

Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 6  
Old 08-01-2016
Thanks rdrtx1, RudiC and R. Singh for awk and sed examples. Working great! The sed example puts letters in array and avoids overwriting by &/; which the awk example does by next.

Good point Don about "h" words. Awk example works by adding:
Code:
/^["hour"]/ {print "an " $0 ; next}

and "a hose" etc. is maintained. But modifying sed example:
Code:
sed 's/^\<hour\>/an &/;

prints: an an hour
# 7  
Old 08-01-2016
Quote:
Originally Posted by p1ne
Thanks rdrtx1, RudiC and R. Singh for awk and sed examples. Working great! The sed example puts letters in array and avoids overwriting by &/; which the awk example does by next.

Good point Don about "h" words. Awk example works by adding:
Code:
/^["hour"]/ {print "an " $0 ; next}

and "a hose" etc. is maintained. But modifying sed example:
Code:
sed 's/^\<hour\>/an &/;

prints: an an hour
No. In awk /^["hour"]/ will select any line starting with ", starting with h, starting with o, starting with u, or starting with r. What you want is something considerably more complex like:
Code:
/^(heir([^s]|$)|honest|honor([^s]|$)|hour([^s]|$))/{print "an " $0; next}
/^(heirs|honors|hours)/{print "the " $0; next}

for US English, or:
Code:
/^(heir([^s]|$)|honest|honour([^s]|$)|hour([^s]|$))/{print "an " $0; next}
/^(heirs|honours|hours)/{print "the " $0; next}

for UK English. Note that I think this will correctly handle cases like:
Code:
an heir
an heiress
the heirs
an heirloom
the heirs' bequests
an heir's bequest
an hour
an hourly
the hours
the honors
an honorific

but I am not at all sure that this list of exceptions is anywhere close to complete.

-----------------------

Update: The above does not take herb into account. And, you can't always tell how to handle it just from the spelling. Some men's names (both Herb and Herbert) have a silent H and some have a verbalized H. Since your code only processes lower case letters, maybe you don't care about proper names.

Last edited by Don Cragun; 08-01-2016 at 11:09 PM.. Reason: Add note and correct to check for heir, honor, and hour at end of input line.
This User Gave Thanks to Don Cragun 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

Find header in a text file and prepend it to all lines until another header is found

I've been struggling with this one for quite a while and cannot seem to find a solution for this find/replace scenario. Perhaps I'm getting rusty. I have a file that contains a number of metrics (exactly 3 fields per line) from a few appliances that are collected in parallel. To identify the... (3 Replies)
Discussion started by: verdepollo
3 Replies

2. UNIX for Dummies Questions & Answers

Search String, Out matched text and input text for no match.

I need to search a string for some specific text which is no big deal using grep. My problem is when the search fails to find the text. I need to add text like "na" when my search does not match. I have tried this command but it does not work when I put the command in a loop in a bash script: ... (12 Replies)
Discussion started by: jojojmac5
12 Replies

3. Shell Programming and Scripting

Grep/Awk on 1st 2 Letters in 2nd Column of File

Hi everyone. I need to change a script (ksh) so that it will grep on the 1st 2 letters in the second column of a 5 column file such as this one: 192.168.1.1 CAXY0_123 10ABFL000001 # Comment 192.168.1.2 CAYZ0_123 10ABTX000002 # Comment 192.168.2.1 FLXY0_123 11ABCA000001 ... (4 Replies)
Discussion started by: TheNovice
4 Replies

4. Shell Programming and Scripting

sed prepend text ?

Hi, I have a file with email header information. I would like to change the Subject line. Subject: ** PROBLEM Host Alert: server.domainname is DOWN ** I'd like to change this line such as to look, Subject: serverID ACK Fw: ** PROBLEM Host Alert: server1.domainname is DOWN** How can I... (2 Replies)
Discussion started by: upengan78
2 Replies

5. Shell Programming and Scripting

How to find the matched numbers between 2 text file using perl program??

hi dudes, I nee you kind assistance, I have to find the matched numbers from 2 text files and output of matched numbers should be in another text file.. I do have text files like this , for example File 1 787 665*5-p 5454 545-p 445-p 5454*-p File 2 5455 787 445-p 4356 2445 144 ... (3 Replies)
Discussion started by: sureshraj
3 Replies

6. Shell Programming and Scripting

How can I get the matched text when using regular expression.

Hello: (exp) : match "exp",the matched text is stored in auto named arrays. How can I get the matched text ? What is the name of the auto named arrays on linux shell ? (4 Replies)
Discussion started by: 915086731
4 Replies

7. Shell Programming and Scripting

get specified part from the grep matched text

hi all, i have some functions in a text file ex:"int* function(int param)" i find this functions with the command "var=grep -o "int**(*)" textfile" i tested this regex and it's working now i want to take only the name of the function from the grep result, in my example "function" how can... (1 Reply)
Discussion started by: bismillah
1 Replies

8. Shell Programming and Scripting

awk text record - prepend first field to all subsequent fields

Hello everyone, I've suddenly gotten very interested in sed and awk (and enjoying it quite a bit too) because of a large conversion project that we're working on. I'm currently stuck with a very inefficient process for processing text blocks. I'm sure someone here should be able to easily point out... (2 Replies)
Discussion started by: jameswatson3
2 Replies

9. Shell Programming and Scripting

Help with uniq -D [prepend]

Hi, I am trying to add a blank line between sets of replicate values. I have been trying to use uniq -D -f4 input.txt > output.txt The input is like V2-1.0 -1.0 5500.00 4162.00 529976.06030125.0 1997A V2-1.0 -1.0 6000.00 4285.00 ... (6 Replies)
Discussion started by: eknryan
6 Replies

10. Shell Programming and Scripting

Append and Prepend Text to a file list

I want to print out a directory listing, then append ] to the end of each line. I'm trying to create a list of Wiki links based on folder listings that i can just copy and paste without having to edit 100's of file listings. Using sed i've figured out to do something like this: sed... (4 Replies)
Discussion started by: CapnDoody
4 Replies
Login or Register to Ask a Question