Visit Our UNIX and Linux User Community

Script to print first and last line have two pattern

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script to print first and last line have two pattern
# 1  
Old 11-10-2009
Script to print first and last line have two pattern

Hello Guys,
I have file like
Line 1
Ser Nu 11223
Line 2
Ser Nu 44556

I need to match line starting with Line and Ser and only display them like

Line 1 Ser Nu 11223 | Line 2 Ser Nu 44556

The number of lines can be multiple Line 1 Line 2 Line 3 Line 4 ........Line 1000

---------- Post updated at 05:58 PM ---------- Previous update was at 04:44 PM ----------

sed -n '/Line/,/Ser/p'
this prints all the lines between the Line and Ser.
A small tweek is needed.........any takers.
# 2  
Old 11-10-2009
awk '/^Line/{s=$0;S++}/^Ser/{printf "%s%s %s",(S>1)?" | ":"",s,$0}' file

---------- Post updated at 05:59 PM ---------- Previous update was at 05:58 PM ----------

To keep the forums high quality for all users, please take the time to format your posts correctly.
  1. Use Code Tags when you post any code or data samples so others can easily read your code.
    You can easily do this by highlighting your code and then clicking on the # in the editing menu. (You can also type code tags [code] and [/code] by hand.)
  2. Avoid adding color or different fonts and font size to your posts.
    Selective use of color to highlight a single word or phrase can be useful at times, but using color, in general, makes the forums harder to read, especially bright colors like red.
  3. Be careful when you cut-and-paste, edit any odd characters and make sure all links are working property.

Thank You.

The UNIX and Linux Forums
Reply With Quote
# 3  
Old 11-10-2009
awk '/^Line/{s=$0;S++}/^Ser/{printf "%s%s %s",(S>1)?" | ":"",s,$0}' aa
awk: syntax error near line 1
awk: illegal statement near line 1

SunOS last-call-5 5.10 Generic_125100-10

aa is the file name
# 4  
Old 11-10-2009
Try /usr/xpg4/bin/awk
# 5  
Old 11-10-2009
Thanks Danmero.
It worked.
If you have a moment to spare any explanation would be great Smilie
Sorry for being greedy Smilie

Previous Thread | Next Thread
Test Your Knowledge in Computers #566
Difficulty: Easy
Many programming languages use two ampersands, a && b to denote the logical And operator.
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Reading a file line by line and print required lines based on pattern

Hi All, i want to write a shell script read below file line by line and want to exclude the lines which contains empty value for MOUNTPOINT field. i am using centos 7 Operating system. want to read below file. # cat /tmp/d5 NAME="/dev/sda" TYPE="disk" SIZE="60G" OWNER="root"... (4 Replies)
Discussion started by: balu1234
4 Replies

2. Shell Programming and Scripting

How to print previous line of multiple pattern matched line?

Hello, I have below format log file, Comparing csv_converted_files/2201/9747.1012H67126.5077292103609547345.csv and csv_converted_files/22019/97447.1012H67126.5077292103609547345.csv Comparing csv_converted_files/2559/9447.1012H67126.5077292103609547345.csv and... (6 Replies)
Discussion started by: arvindshukla81
6 Replies

3. Shell Programming and Scripting

Match Pattern and print pattern and multiple lines into one line

Hello Experts , require help . See below output: File inputs ------------------------------------------ Server Host = mike id rl images allocated last updated density vimages expiration last read <------- STATUS ------->... (4 Replies)
Discussion started by: tigerhills
4 Replies

4. Shell Programming and Scripting

Print only next pattern in a line after a pattern match

I have 2013-06-11 23:55:14 1Umexd-0004cm-IG <= I need sed/awk operation on this, so that it should print the very next pattern only after the the pattern mach <= ie only print (7 Replies)
Discussion started by: anil510
7 Replies

5. Shell Programming and Scripting

Script to compare pattern and print a different pattern in each line

Hi, I am writing a shell script to parse some files, and gather data. The data in the files is displayed as below. abz: ......qrt: .... abz: ......qrt: ... I have tried using awk and cut, but the position of these values keep changing, so I wasn't able to get... (2 Replies)
Discussion started by: Serena
2 Replies

6. Shell Programming and Scripting

Print between multi line pattern

Hi, I have a file with text like this .SET WIDTH 10000 .SET MAXERROR 1 insert into new_db SELECT * FROM some_db ; +---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+- .SET WIDTH 10000... (3 Replies)
Discussion started by: sol_nov
3 Replies

7. Shell Programming and Scripting

Print Line if next line Match a pattern

Hi All, Does anyone know how to print 1H1A....... in peal script print line ^1H1A....... if next line equal 5R0RECEIPT.... Thank for help:D Cat st.txt 1H1A-IN-11-5410-0009420|1010047766|dsds|1|N|IN|IN|000000|1||N|<<<line match 5R0RECEIPT| 5R0RECEIPT|... (2 Replies)
Discussion started by: kittiwas
2 Replies

8. Programming

Print specific pattern line in c++

Discussion started by: cpp_beginner
10 Replies

9. Shell Programming and Scripting

match a pattern and print the line once

Hi, I have a xml file <cisco:name> <cisco:mdNm>Cisco Device 7500 A Series</cisco:mdNm> <cisco:meNm></cisco:meNm> <cisco:ehNm>/shelf=1</cisco:ehNm> <cisco:subname> <cisco:meNm></cisco:meNm> <cisco:sptp>Cisco PortA Series</cisco:sptp> ... (11 Replies)
Discussion started by: bhagirathi
11 Replies

10. Shell Programming and Scripting

Print the line within the search pattern

Hi Guys, I had file as typedef struct { char TrailerType1; char TrailerTxt1; }Trailer; typedef struct { char PfigMoneyType; char PfigMoneyvalue; }PfigMoney; i need to print the lines within the search pattern. if i give the search pattern as... (3 Replies)
Discussion started by: manosubsulo
3 Replies

Featured Tech Videos