Using awk or sed to find a pattern that has lines before and after it


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Using awk or sed to find a pattern that has lines before and after it
# 1  
Old 05-25-2016
Using awk or sed to find a pattern that has lines before and after it

Dear gurus,
Please help this beginner to write and understand the required script. I am looking for useing awk for sed.

I have a few thousand lines file whose contain are mostly as below and I am trying to achieve followings.
1. Find a string, say user1. Then hash the line containing the string along with 2 lines before and 1 line after.
2. The script should not hast other lines like user10,11,12 etc.

The file looks like as below.
Code:
        start {
                UID 5001
                NAME  user1
                }
        start {
                UID 5002
                NAME  user2
                }
        start {
                UID 5003
                NAME  user3
                }
        start {
                UID 5004
                NAME  user4
                }
        start {
                UID 5005
                NAME  user5
                }
        start {
                UID 5006
                NAME  user6
                }
        start {
                UID 5007
                NAME  user7
                }
        start {
                UID 5008
                NAME  user8
                }
        start {
                UID 5009
                NAME  user9
                }
        start {
                UID 5010
                NAME  user10
                }
        start {
                UID 5011
                NAME  user11
                }
        start {
                UID 5012
                NAME  user12
                }
        start {
                UID 5013
                NAME  user13
                }
        start {
                UID 5014
                NAME  user14
                }
        start {
                UID 5015
                NAME  user15
                }
        start {
                UID 5016
                NAME  user16
                }
        start {
                UID 5017
                NAME  user17
                }
        start {
                UID 5018
                NAME  user18
                }
        start {
                UID 5019
                NAME  user19
                }
        start {
                UID 5020
                NAME  user20
                }

Thanks in advance
Randy

Last edited by Scott; 05-25-2016 at 08:15 PM.. Reason: Please use code tags
# 2  
Old 05-25-2016
Is this a homework assignment? If not, please show your efforts so far.
# 3  
Old 05-26-2016
Code:
for i in user1 user4 usern
do
grep -B2 "$i" file | sed 's/^/#/g'
grep "$i" file | sed 's/^/#/g'
grep -A1 "$i" file | sed 's/^/#/g'
done

# 4  
Old 05-26-2016
Try:
Code:
printf '%s\n' user11 user1 user4 | awk '
NR == FNR {
	list[$1]
	next
}
$6 in list {
	printf("%s}\n", substr($0, 1 + (FNR > 1)))
}' - RS='}' file

which, with your sample input file, produces the output:
Code:
        start {
                UID 5001
                NAME  user1
                }
        start {
                UID 5004
                NAME  user4
                }
        start {
                UID 5011
                NAME  user11
                }

If you want to try this on a Solaris/SunOS system, change awk to /usr/xpg4/bin/awk or nawk.
# 5  
Old 05-26-2016
Thanks to all for reply and my sincere apology to Scott for not showing the work.
As I mentioned you the file is pretty big and I am trying to update the source file at the same time too. I did some search and understand that sed buffer option can handle it but frankly could not understand.

Thanks
Randy
# 6  
Old 05-26-2016
Quote:
Originally Posted by ran_bon_78
I have a few thousand lines file whose contain are mostly as below and I am trying to achieve followings.
1. Find a string, say user1. Then hash the line containing the string along with 2 lines before and 1 line after.
2. The script should not hast other lines like user10,11,12 etc.

[...]
Code:
perl -ne 'BEGIN{$/="}\n"} /user1$/m and print' example.data

Output:
Code:
        start {
                UID 5001
                NAME  user1
                }

Substitute the red for any other you want. If you want several users like user1 and user4, change to /user(1|4)$/m

It works also with UID as well:

Code:
perl -ne 'BEGIN{$/="}\n"} /5002$/m and print' example.data

Output:
Code:
        start {
                UID 5002
                NAME  user2
                }


Last edited by Aia; 05-26-2016 at 02:37 PM.. Reason: Add UID example
# 7  
Old 05-26-2016
Quote:
Originally Posted by ran_bon_78
Thanks to all for reply and my sincere apology to Scott for not showing the work.
As I mentioned you the file is pretty big and I am trying to update the source file at the same time too. I did some search and understand that sed buffer option can handle it but frankly could not understand.

Thanks
Randy
You never answered Scott's main question. Is this a homework assignment? If this is not homework, why are we restricted to "useing(sic) awk for(sic) sed"?

And, until now, you never said anything about wanting to update the file. You said you wanted to hash certain lines and did not want to hast(?) other lines. If you want to update certain strings in your file, what replacement text do you want to use? And, why do you care about "hash"ing lines you don't want to change?

How is your script supposed to determine which strings are to be replaced and what replacement text is to be used? Do you intend to hard code values into your script and edit your script every time before you run it? Will you be passing in pairs of command-line arguments specifying old and new text? Will you supply an input file that specifies old and new values? (If so, what will be used as the separator between old and new values in that file? And, what will be the name of that file (or will the name of that file be given as an operand)?)

What shell are you using?

What operating system are you using?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

find pattern matches in consecutive lines in certain fields-awk

I have a text file with many thousands of lines, a small sample of which looks like this: InputFile:PS002,003 D -1 5 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 6 6 -1 -1 -1 -1 0 509 0 PS002,003 PSQ 0 1 7 18 1 0 -1 1 1 3 -1 -1 ... (5 Replies)
Discussion started by: jvoot
5 Replies

2. UNIX for Beginners Questions & Answers

Sed/awk join lines once pattern found

Hi all OS - RHEL6.4 I have input file -f1.txt I need to search line which starts with \Start and read next line till it gets blank line and join them all. I need to trim any trailing spaces for each line.So output.txt should be.. \Start\now\fine stepwatch this space for toolsends... (7 Replies)
Discussion started by: krsnadasa
7 Replies

3. Shell Programming and Scripting

sed -- Find pattern -- print remainder -- plus lines up to pattern -- Minus pattern

The intended result should be : PDF converters 'empty line' gpdftext and pdftotext?xml version="1.0"?> xml:space="preserve"><note-content version="0.1" xmlns:/tomboy/link" xmlns:size="http://beatniksoftware.com/tomboy/size">PDF converters gpdftext and pdftotext</note-content>... (9 Replies)
Discussion started by: Klasform
9 Replies

4. Shell Programming and Scripting

[awk] find pattern, change next two lines

Hi, hope you can help me... It seems like a straightforward problem, but I haven't had any success so far using my basic scripting and awk "skills": I need to find a pattern /VEL/ in an input file that looks like this: 1110SOL OW25489 1.907 7.816 26.338 -0.4365 0.4100 -0.0736 ... (3 Replies)
Discussion started by: origamisven
3 Replies

5. Shell Programming and Scripting

Getting lines before and until next pattern in file /awk, sed

Hi, I need to get specific parts in a large file. I need to: Get a line containing an IP address, and read from there to another line saying ***SNMP-END*** So, I have the start and the end well defined, but the problem is that apparently the awk command using the -F option doesn't work... (17 Replies)
Discussion started by: ocramas
17 Replies

6. Shell Programming and Scripting

How to print the lines between the pattern using awk/grep/sed?

Hi, I need a help to search a pattern and print the multiple lines between them. Input file: Tue May 29 12:30:33 EDT 2012:threadWebContainer : 357:com.travimp.hotelierlinks.abba.service.RequestHandler.requestService(String, ITICSDataSet): hotelCancelReservation request: ... (4 Replies)
Discussion started by: aroragaurav.84
4 Replies

7. Shell Programming and Scripting

sed/awk : how to delete lines based on IP pattern ?

Hi, I would like to delete lines in /etc/hosts on few workstations, basically I want to delete all the lines for a list of machines like this : for HOST in $(cat stations.lst |uniq) do # echo -n "$HOST" if ping -c 1 $HOST > /dev/null 2>&1 then HOSTNAME_val=`rsh $HOST "sed... (3 Replies)
Discussion started by: albator1932
3 Replies

8. Shell Programming and Scripting

awk to find pattern and add lines

My file goes like this: SID_LIST_HOSTNAME_LISTENER_3 = (SID_LIST = (SID_DESC = (SID_NAME = ORA0008) (ORACLE_HOME = /opt/oracle/product/ORA0008) (ENVS = "LD_LIBRARY_PATH=/opt/oracle/product/ORA0008/lib") ) (SID_DESC = (SID_NAME = ORA0007) ... (4 Replies)
Discussion started by: jpsingh
4 Replies

9. Shell Programming and Scripting

sed/awk to insert multiple lines before pattern

I'm attempting to insert multiple lines before a line matching a given search pattern. These lines are generated in a separate function and can either be piped in as stdout or read from a temporary file. I've been able to insert the lines from a file after the pattern using: sed -i '/pattern/... (2 Replies)
Discussion started by: zksailor534
2 Replies

10. Shell Programming and Scripting

How to awk/sed/grep lines which contains a pattern at a given position

Dear friends I am new to linux and was trying to split some files userwise in our linux server. I have a data file of 156 continuous columns named ecscr final. I want the script to redirect all the lines containing a pattern of 7 digits to separate files. I was using grep to do that,... (2 Replies)
Discussion started by: anoopvraj
2 Replies
Login or Register to Ask a Question