Awk-sed help : to remove first and last line with pattern match:


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Awk-sed help : to remove first and last line with pattern match:
# 1  
Old 01-22-2013
Awk-sed help : to remove first and last line with pattern match:

awk , sed Experts,

I want to remove first and last line after pattern match "vg" :
I am trying : # sed '1d;$d' works fine , but where the last line is not having vg entry it is deleting one line of data.
- So it should check for the pattern vg if present , then it should delete the line , else do nothing.

file1 :
Code:
/dev/vgP2XT
/dev/disk/disk4415
/dev/disk/disk4335
/dev/disk/disk4214
/dev/disk/disk4413
/dev/disk/disk4423
/dev/disk/disk41313
/dev/disk/disk21063
/dev/disk/disk20702
/dev/disk/disk21423
/dev/disk/disk21122
/dev/vgQ4XT



I have tried: the above sed on the file2 , but cutting one data line, that I don't want in the output :

file2 :
Code:
/dev/vgP2XT
/dev/disk/disk4415
/dev/disk/disk4335
/dev/disk/disk4214
/dev/disk/disk4413
/dev/disk/disk4423
/dev/disk/disk41313
/dev/disk/disk21063
/dev/disk/disk20702
/dev/disk/disk21423
/dev/disk/disk21122


Tried:
sed '1d;$d' , but it is deleting last line "disk21122"
How to tell sed to check for the pattern, and then delete, if possible.




So I want the output should be:

Code:
/dev/disk/disk4415
/dev/disk/disk4335
/dev/disk/disk4214
/dev/disk/disk4413
/dev/disk/disk4423
/dev/disk/disk41313
/dev/disk/disk21063
/dev/disk/disk20702
/dev/disk/disk21423
/dev/disk/disk21122


Thanks,
# 2  
Old 01-22-2013
Try:
Code:
grep '/dev/d'

Code:
grep -v '/dev/vg'

--
or, if it needs to be only on the 1st and the last line:
Code:
sed '1{\|/dev/vg|d;}; ${//d;}' infile


Last edited by Scrutinizer; 01-22-2013 at 03:35 PM..
This User Gave Thanks to Scrutinizer For This Post:
# 3  
Old 01-22-2013
Hi Scrutinizer ,
Thanks, but the sed did not work well,
please advise, how not to print, first and last line if pattern matches to the first and last line.


Code:
# cat infile
/dev/vgP2XT
/dev/disk/disk4415
/dev/disk/disk4335
/dev/disk/disk4214
/dev/disk/disk4413
/dev/disk/disk4423
/dev/disk/disk41313
/dev/disk/disk21063
/dev/disk/disk20702
/dev/disk/disk21423
/dev/disk/disk21122
/dev/vgQ4XT





Code:
# sed '1{\|/dev/vg|d;}; ${//d;}' infile   
/dev/disk/disk4415
/dev/disk/disk4335
/dev/disk/disk4214
/dev/disk/disk4413
/dev/disk/disk4423
/dev/disk/disk41313
/dev/disk/disk21063
/dev/disk/disk20702
/dev/disk/disk21423
/dev/disk/disk21122
/dev/vgQ4XT
#


It did not removed the last "vg" pattern matched entry. /dev/vgQ4XT
# 4  
Old 01-23-2013
Try this instead:
Code:
sed '\|/dev/vg|{1d; $d;}' infile

This User Gave Thanks to Scrutinizer For This Post:
# 5  
Old 01-24-2013
Thanks Scrutinizer, this works great .


Code:
sed '\|/dev/vg|{1d; $d;}' infile

I am not able to understand why "| | " used in between ,
Could please explain , Thanks again.
# 6  
Old 01-24-2013
Hi revri, \| .. | is the same as / .. / , the difference is that the / characters in /dev/vg would need to be escaped:
Code:
sed '/\/dev\/vg/{1d; $d;}' infile

You can use most characters as long as the first one is escaped:
Code:
sed '\:/dev/vg:{1d; $d;}' infile

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

awk with sed to combine lines and remove specific odd # pattern from line

In the awk piped to sed below I am trying to format file by removing the odd xxxx_digits and whitespace after, then move the even xxxx_digit to the line above it and add a space between them. There may be multiple lines in file but they are in the same format. The Filename_ID line is the last line... (4 Replies)
Discussion started by: cmccabe
4 Replies

2. Shell Programming and Scripting

Get range out using sed or awk, only if given pattern match

Input: START OS:: UNIX Release: xxx Version: xxx END START OS:: LINUX Release: xxx Version: xxx END START OS:: Windows Release: xxx Version: xxx ENDHere i am trying to get all the information between START and END, only if i could match OS Type. I can get all the data between the... (3 Replies)
Discussion started by: Dharmaraja
3 Replies

3. Shell Programming and Scripting

sed to remove newline chars based on pattern mis-match

Greetings Experts, I am in AIX; I have a file generated through awk after processing the input files. Now I need to replace or remove the new-line characters on all lines that doesn't have a ; which is the last character on the line. I tried to use sed 's/\n/ /g' After checking through the... (6 Replies)
Discussion started by: chill3chee
6 Replies

4. Shell Programming and Scripting

awk to combine lines from line with pattern match to a line that ends in a pattern

I am trying to combine lines with these conditions: 1. First line starts with text of "libname VALUE db2 datasrc" where VALUE can be any text. 2. If condition1 is met then continue to combine lines through a line that ends with a semicolon. 3. Ignore case when matching patterns and remove any... (5 Replies)
Discussion started by: Wes Kem
5 Replies

5. Shell Programming and Scripting

Awk; pattern match, remove and re write

the following pattern match works correctly for me awk '/name="Fruits"/{f=1;next} /"name=Vegetables"/{f=0} f' filename This works well for me. Id like to temporarily move the match out of the file ( > newfile) and be able to stick it back in the same place at a later time. Is this... (7 Replies)
Discussion started by: TY718
7 Replies

6. Shell Programming and Scripting

Pattern match with awk/sed - help

I need to grep for the pattern text inside the square brackets which are in red and not in green..my current code greps patterns both of them, which i don't want Input fileref|XP_002371341.1| oxoacyl-ACP reductase, putative gb|EPT24759.1| 3-ketoacyl-(acyl-carrier-protein) reductase ... (2 Replies)
Discussion started by: selvankj
2 Replies

7. Shell Programming and Scripting

Sorting content between match pattern and move on with awk and sed

S 0.0 0.0 (reg, inst050) k e f d c S 0.0 0.0 (mux, m030) k g r s x v S 0.0 0.0 (reg, inst020) q s n m (12 Replies)
Discussion started by: ctphua
12 Replies

8. Shell Programming and Scripting

awk print pattern match line and following lines

Data: Pattern Data Data Data Data Data Data Data Data Data ... With awk, how do I print the pattern matching line, then the subsequent lines following the pattern matching line. Varying number of lines following the pattern matching line. (9 Replies)
Discussion started by: dmesserly
9 Replies

9. Shell Programming and Scripting

I need to know how to replace a line after a pattern match with an empty line using SED

Hi How Are you? I am doing fine! I need to go now? I will see you tomorrow! Basically I need to replace the entire line containing "doing" with a blank line: I need to the following output: Hi How Are you? I need to go now? I will see you tomorrow! Thanks in advance.... (1 Reply)
Discussion started by: sags007_99
1 Replies

10. Shell Programming and Scripting

sed - Replace Line which contains the Pattern match with a new line

I need to replace the line containing "STAGE_DB" with the line "STAGE_DB $DB # database that contains the table being loaded ($workingDB)" Here $DB is passed during the runtime. How can I do this? Thanks, Kousikan (2 Replies)
Discussion started by: kousikan
2 Replies
Login or Register to Ask a Question