Reputation: 640
I have data in a file in below form :--
The symbol ":=" was substituted for "QUERY" to continue.
SQL> _id : MS
itm : 4
it : NO
------
PL/SQL procedure successfully completed.
I want to delete everything before _id and after ------ .
To delete everything before _id I used following sed
sed '/_id/,$!d' 1.txt
It deletes the row before _id but doesnt delete SQL> .
Similarly I used below sed to delete all everything after ------ but it doesnt delete the rows below it
sed 's/\------.*/------/' 1.txt
Can someone help me where I am doing wrong ? What I need is :--
_id : MS
itm : 4
it : NO
Upvotes: 1
Views: 836
Reputation: 113814
Try:
$ sed -n '/_id/,/------/{ s/.*_id/_id/; /------/q; p}' 1.txt
_id : MS
itm : 4
it : NO
How it works:
-n
This tells sed not to print unless we explicitly ask it to.
/_id/,/------/{...}
This selects a range of lines that start with a line containing _id
and end with a line containing ------
. For those lines the commands in curly braces are executed.
s/.*_id/_id/
On the line that contains _id
, this removes everything before _id
.
/------/q
On the line that contains ------
, this tells sed to quit.
p
For lines that reach this command, this tells sed to print the line.
$ awk -v RS='------' '/_id/{sub(/.*_id/, "_id"); print}' ORS="" 1.txt
_id : MS
itm : 4
it : NO
How it works:
-v RS='------'
This set ------
as the record separator.
/_id/{sub(/.*_id/, "_id"); print}
For any records that contain _id
, we remove everything before _id
and print the current record.
ORS=""
This sets the output record separator to the empty string.
Upvotes: 2