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

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.

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.

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

:):confused:

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.

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.

Test file:

$ 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:

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.

1 Like

To the test file you have posted above, try something like..

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 :b:

1 Like

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:

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.

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

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

Data samples are worth a thousand words. Show us the code you used and how you invoked it. Show us the input you fed to that code. Show us the actual output of that code. Show us the desired output for that input. If there is anything that isn't obvious, then please explain further.

Explanations are helpful -- they help us digest the data samples faster -- but they are no substitute for demonstrative data.

Regards,
Alister

@michaelrozar17

It has inserted multiple lines. Instead of inserting the new line after the last pattern, Is it possible to insert the new line according to the Alphabetical order.

root@bmcpunscm-lnx-test: /root/projects/user_creations-> sed '/ARS (11)/!s/.*/SOME TEXT HERE\n&/' forum
paul, Paul Smith, Stevn Smiley, REQ000001, ARS (11)
sam, Sam Martin, Stevn Smiley, REQ000001, ARS (11)
mike, Mike Conway, Stevn Smiley, REQ000001, ARS (11)
carl, carl Conway, Stevn Smiley, REQ000001, ARS (11)
SOME TEXT HERE
#
SOME TEXT HERE
# Atrium #
SOME TEXT HERE
steve, Steven Seagul, Stevn Smiley, REQ000001, ATRIUM (11)