Prepend text, different matched 1st letters


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Prepend text, different matched 1st letters
# 8  
Old 08-02-2016
Quote:
Originally Posted by p1ne
.
.
.
But modifying sed example:
Code:
sed 's/^\<hour\>/an &/;

prints: an an hour
No, it does not. Only if embedded in the sed script given, it will, in two steps: first hour is replaced by an hour, then an is replaced by an an. And this is exactly what I said above: You'll need to keep it from being modified twice.
# 9  
Old 08-02-2016
I'm sorry, by modifying sed example I meant embedded in your example prints "an an hour." I understand why (2nd statement matching "(a)n" from 1st...so how to fix?
Code:
sed 's/^\<hour\>/an &/; s/^[aeiou]/an &/; s/^[bcdfghjklmnpqrstvwxyz]/a &/ ' test > test2

---------- Post updated at 08:38 AM ---------- Previous update was at 08:27 AM ----------
Don:
Thanks for taking the time to demonstrate awk example. I must have been confused before when I ran array ["hour"] and thought I saw the correct result. Just re-tested and indeed it doesn't work. So pipes | are used for complete words.


# 10  
Old 08-02-2016
You might want to make sed quit after the first modification.
# 11  
Old 08-02-2016
Quote:
Originally Posted by p1ne
I'm sorry, by modifying sed example I meant embedded in your example prints "an an hour." I understand why (2nd statement matching "(a)n" from 1st...so how to fix?
Code:
sed 's/^\<hour\>/an &/; s/^[aeiou]/an &/; s/^[bcdfghjklmnpqrstvwxyz]/a &/ ' test > test2

---------- Post updated at 08:38 AM ---------- Previous update was at 08:27 AM ----------
Don:
Thanks for taking the time to demonstrate awk example. I must have been confused before when I ran array ["hour"] and thought I saw the correct result. Just re-tested and indeed it doesn't work. So pipes | are used for complete words.
The pipe symbols in the extended regular expressions used in awk (and grep -E and some other utilities) separate alternatives. They don't have to be complete words. The line of code I suggested:
Code:
/^(heirs|honors|hours)/{print "the " $0; next}

could also be written as:
Code:
/^h(eir|onor|our)s/{print "the " $0; next}

(which looks for the letter "h" at the start of a line followed by one of the strings "eir", "onor", or "our" followed by the letter "s") and get exactly the same results with less typing. I just find the first form easier for many of the novices who read this forum to understand. (And the longer form keeps automatic spell checkers from trying to correct non-existent typos. Smilie )
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