Grep search and append


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Grep search and append
# 1  
Old 06-27-2011
Grep search and append

I'm sure glad I found this forum... This is my first post, so please be gentle... Smilie

I tried searching everywhere, but the terminology is so common that I cannot find a solution to my problem.

I'm looking for a GREP statement to do the following...

I need a search and append function that will search for the word "The " at the beginning of a line, and if it exists, trim it, and append it to the end of the line.

(note: it needs to ensure the first 4 chars are "the " and not modify fields with "the" in the middle of the text.

Two examples:

BEFORE:
The Beatles
The Beginning and the End

AFTER:
Beatles, The
Beginning and the End, The


Can anyone point me to a good example, or help me out? It would be much appreciated!

Thanks in advance!
# 2  
Old 06-27-2011
Code:
perl -pe 's/^(The )(.*)/\2, \1/' file

# 3  
Old 06-27-2011
Code:
% cat > testfile
The Beatles
The Beginning and the End

% sed 's/^The //; s/$/, The/' testfile
Beatles, The
Beginning and the End, The

Or better:
Code:
% sed '/^The / {s///; s/$/, The/}' testfile

# 4  
Old 06-27-2011
Code:
 
bash-3.00$ nawk ' /^The / {for(i=2;i<=NF;i++) { if(i==NF) printf ("%s, %s\n",$NF,$1); else printf("%s ",$i);}}' test
Beatles, The
Beginning and the End, The

# 5  
Old 06-27-2011
Code:
sed 's/^The \(.*\)/\1 ,The/' file

# 6  
Old 06-27-2011
Thanks very much, guys!
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to use a grep search to search for a specific string within multiple directories?

Lets say I have a massive directory which is filled with other directories all filled with different c++ scripts and I want a listing of all the scripts that contain the string: "this string". Is there a way to use a grep search for that? I tried: grep -lr "this string" * but I do not... (3 Replies)
Discussion started by: Circuits
3 Replies

2. Shell Programming and Scripting

Search and Replace+append a text in python

Hello all, I have a verilog file as following (part of it): old.v: bw_r_rf16x32 AUTO_TEMPLATE ( 1957 // .rst_tri_en (mem_write_disable), 1958 .rclk (clk), 1959 .bit_wen (dva_bit_wr_en_e), 1960 .din ... (5 Replies)
Discussion started by: Zam_1234
5 Replies

3. Shell Programming and Scripting

Grep and append

Have a string as below in a file. "anohter boy has id 000921 and girl has id=655 of roll number" using grep below commands with grep commands but able to get one string at a time, not able to append. less <filename> | grep -o -P '(?<=id ).*(?=and )' less <filename> | grep -o -P... (1 Reply)
Discussion started by: Satyak
1 Replies

4. Shell Programming and Scripting

Search id from second file and append in first

Hello, I want to search a string/substring from the second column in file in another file and append the first found record in second file to the end of the record in the first file. Both files are tab delimited. All lines with KOG in col13 do not need to be searched as it will not be... (7 Replies)
Discussion started by: gina.lizar
7 Replies

5. Shell Programming and Scripting

Search text and append using SED?

I have file . cat hello.txt Hello World I would like to append a string "Today " so the output is cat hello.txt Hello World Today I dont know which line number does the "Hello World" appears otherwise I could have used the Line number to search and append . (3 Replies)
Discussion started by: gubbu
3 Replies

6. Shell Programming and Scripting

Search for a particular field length and append '0' if less less than 10

Hi, I am new to Unix. Please help me in finding solution for the below scenario. I have a flat file that contains data like 378633410|3013505414|new_378633410|ALBERT|WALLS|378633410|Rew||||||| 351049045|239|new_351049045|JIM|COOK|351049045|Rew|||||||... (6 Replies)
Discussion started by: anandek
6 Replies

7. Shell Programming and Scripting

How to search and append words in a file

Hi , I have a file myhost.txt which contains below, 127.0.0.1 localhost 1.17.1.5 atrpx958 11.17.10.11 atrpx958zone nsybhost I need to append words only after "atrpx958" like 'myhost' and 'libhost' and not after atrpx958zone. How to search the word atrpx958 only in... (2 Replies)
Discussion started by: gsreeni
2 Replies

8. Shell Programming and Scripting

Trying to search for a string and append text only once

Hi I am trying to search for a particular occurrence of a string in a file, and if found, append another string to the end of that line. Here is my file contents: column1 userlist default nowrite=3 output=4 column2 access default nowrite=3 Here is the code: A="user=1... (1 Reply)
Discussion started by: bludhemn
1 Replies

9. Shell Programming and Scripting

Perl search and append new line

Dear All, I want search two lines and append some string in between these lines. Input file tmp,123 ,10:123 tmp,666 ,50:999 tmp,2:19800 5,3:21. tmp,2:19800 55555555 tmp,2:19800 5,3:21.Output should be tmp,123 ,10:123 tmp,666 ,50:999 tmp,2:19800 (4 Replies)
Discussion started by: arvindng
4 Replies

10. Shell Programming and Scripting

Search and Append

All, I stuck up for the logic, how to implement the below thing in script. -Search for <a class="string-array"> line and in next line of search string append with <string>java</string> in a xml file The problem here is, <a class="string-array"> occurs multiple places in the xml. I just... (13 Replies)
Discussion started by: vino_hymi
13 Replies
Login or Register to Ask a Question