Script using Sed :Search all patterns & after the last Patter, insert a newLine with Comma Sep Value


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script using Sed :Search all patterns & after the last Patter, insert a newLine with Comma Sep Value
# 1  
Old 07-31-2012
Script using Sed :Search all patterns & after the last Patter, insert a newLine with Comma Sep Value

I am trying to search the pattern "ARS (11)" and after the LAST pattern, i am trying to open new line and enter text using sed.

My Existing Text file is Users.txt
Code:
paul, Paul Smith, Stevn Smiley, REQ000001, ARS (11)
sam, Sam Martin, Stevn Smiley, REQ000001, ARS (11)
mike, Mike Conway, Stevn Smiley, REQ000001, ARS (11)

On Users.txt, I want the sed to Insert a New Line after the LAST "ARS (11)" pattern. In this case, fourth line is inserted by sed.
Code:
paul, Paul Smith, Stevn Smiley, REQ000001, ARS (11)
sam, Sam Martin, Stevn Smiley, REQ000001, ARS (11)
mike, Mike Conway, Stevn Smiley, REQ000001, ARS (11)
bob, Robert Hayward, Stevn Smiley, REQ000001, ARS (11)

I have tried the following command and however it is not working.
Code:
cat /root/projects/user_creations/Users.txt | tail -1 | while read line
do
#echo "$line" | sed -e '/ARS (11)/a\'$user''
echo "$line" | sed '/ARS (11)/a\"$user"'
done

SmilieSmilie
Moderator's Comments:
Mod Comment Code tags for code, please.

Last edited by Corona688; 07-31-2012 at 12:15 PM..
# 2  
Old 07-31-2012
There is no need to use cat there. See Useless Use of Cat.

Use code tags instead of indents for code, please.

Why not just append to the end of your file if that's all your input file contains? You don't need to search for anything.
# 3  
Old 07-31-2012
Thanks for your reply.

The file contains many other Patterns like "ATRIUM (11)" ""BL (11)" Etc, hence redirecting will take it to the last line and sorting also didn't help.
# 4  
Old 07-31-2012
Test file:

Code:
$ cat test
paul, Paul Smith, Stevn Smiley, REQ000001, ARS (11)
sam, Sam Martin, Stevn Smiley, REQ000001, ARS (11)
mike, Mike Conway, Stevn Smiley, REQ000001, ARS (11)
mike, Mike Conway, Stevn Smiley, REQ000001, ARS (12)

Results:
Code:
sed "$(awk -F',' '$5 ~ /.*ARS\ \(11\)/ {print NR}' test | tail -1)aTest String of text" test
paul, Paul Smith, Stevn Smiley, REQ000001, ARS (11)
sam, Sam Martin, Stevn Smiley, REQ000001, ARS (11)
mike, Mike Conway, Stevn Smiley, REQ000001, ARS (11)
Test String of text
mike, Mike Conway, Stevn Smiley, REQ000001, ARS (12)

Used awk to get the line numbers of all the regex, then just grabbed the last one and threw it to sed in append mode.
This User Gave Thanks to Vryali For This Post:
# 5  
Old 08-01-2012
To the test file you have posted above, try something like..
Code:
sed '/ARS (11)/!s/.*/SOME TEXT HERE\n&/' test_file

Note: This would not work for all scenarios until there is a exact sample input/test file Smilie
This User Gave Thanks to michaelrozar17 For This Post:
# 6  
Old 08-01-2012
How large/big will be the input file in terms of the number of lines and the length of each line?

---------- Post updated at 02:16 AM ---------- Previous update was at 01:56 AM ----------

If it's not a huge file or if your machine has sufficient memory, you may try:
Code:
sed -n 'H
$ {
 g
 s/^\n//
 s:\(.*ARS (11)\)\n:\1\
SOME TEXT\
:
 s:\(.*ARS (11)\)$:\1\
SOME TEXT:
 p
}' infile

I've assumed that "ARS (11)" is expected at the end of a line.
# 7  
Old 08-01-2012
@Vryali,
Thanks a lot and the code worked fine. However, Is it possible to insert the text in a Alphabetical Order instead of inserting it in the Last line.

Code:
sed "$(awk -F',' '$5 ~ /.*ARS\ \(11\)/ {print NR}' test | tail -1)aTest String of text" test

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bring values in the second column into single line (comma sep) for uniq value in the first column

I want to bring values in the second column into single line for uniq value in the first column. My input jvm01, Web 2.0 Feature Pack Library jvm01, IBM WebSphere JAX-RS jvm01, Custom01 Shared Library jvm02, Web 2.0 Feature Pack Library jvm02, IBM WebSphere JAX-RS jvm03, Web 2.0 Feature... (10 Replies)
Discussion started by: kchinnam
10 Replies

2. Shell Programming and Scripting

How to replace NewLine with comma using sed?

Hi , I have a huge file with following records and I want to replace the last comma with ',NULL'. I try using SED but could not create a correct script . In my opinion I need a script which can convert ,/n with ,NULL/n 1,CHANGE_MEMBER,2010-12-28 00:05:00, 2,CHANGE_MEMBER,2012-09-02... (8 Replies)
Discussion started by: Ajaypal
8 Replies

3. Shell Programming and Scripting

String search between patterns using sed

Hi, I am trying to find a way to get sed/awk/grep to help me find a string in a log file that exists between two datestamps and then print the preceding datestamp up to the next datestamp. Here is an example of my logfile: +++ 2013/03/28 17:01:37.085 SIGNALING HIGH ACTIVE Failure Response... (5 Replies)
Discussion started by: raytx
5 Replies

4. Shell Programming and Scripting

help with tar & zip only last months(say,Sep) files

Need to 1. archive all the files in a directory from the previous month into a tar/gz file, ignoring all already archived 'tar.gz' files 2. Check created .tar.gz file isnt corrupted and has all the required files in it. and then remove the original files. I am using a function to get the... (1 Reply)
Discussion started by: Prev
1 Replies

5. Shell Programming and Scripting

awk: Multiple search patterns & print in an one liner

I would like to print result of multiple search pattern invoked from an one liner. The code looks like this but won't work gawk -F '{{if ($0 ~ /pattern1/) pat1=$1 && if ($0 ~ /pattern2/) pat2=$2} ; print pat1, pat2}' Can anybody help getting the right code? (10 Replies)
Discussion started by: sdf
10 Replies

6. Shell Programming and Scripting

sed insert text without newline

Hi, I use sed to insert text at beginning of a file. But sed inserts a newline after my text that I do not need. For example, I want to insert "foo" at the beginning of my file: > cat myfile This is first line. > sed -i '1i\foo' myfile > cat myfile foo This is first line. ... (5 Replies)
Discussion started by: tdw
5 Replies

7. Shell Programming and Scripting

AWK Script Issue insert newline for a regular expression match

Hi , I am having an issue with the Awk script to insert newline for a regular expression match Having a file like this FILE1 #################### RXOER , RXERA , RXERC , RXERD .RXEA(RXBSN), RXERD , REXCD input RXEGT buffer RXETRY ####################### Want to match the RXE... (38 Replies)
Discussion started by: jaita
38 Replies

8. Shell Programming and Scripting

sed search alternately for patterns

hi all, I am trying to do search on a gzip file. The file has <pattern1> Data.. <pattern2> data <pattern1> data <patter2> I want to print each pattern 1 and the corrresponding pattern2. If pattern 2 fails to appear and pattern 1 appears, I do not want to print pattern1 and... (3 Replies)
Discussion started by: baskar123
3 Replies

9. Shell Programming and Scripting

Awk Script: Nested search using different patter

Hi Friends. Please have a look at dummy file. I need to extract from this file: 1. Counts of event= 2. the 2nd coulmn is unique call id of this transaction. Based on that, i have to search for txstatus= . Note: Values of event, calltype and txstatus can be anything. I want to print... (1 Reply)
Discussion started by: itsmesanju
1 Replies

10. Shell Programming and Scripting

need to get the last word in comma sep line

I have a file with aaa,bbb,ccc,dddd,eee,xyz aaa,bbb,ccc,dddd,eee,xyz,12345,rty aaa,bbb,ccc,dddd,eee,xyz,12345,rty,tsrt 1. line columns are not fixed 2. all words are seperated by comma what i want is always the string after last comma. regards, Senthil... (9 Replies)
Discussion started by: senthilk615
9 Replies
Login or Register to Ask a Question