PDA

View Full Version : [SOLVED] Delete all lines before a specific word. Unix Scripting



hakermania
July 23rd, 2010, 07:05 PM
Let's say we have a file containing:

alllllsadfsdasdf
qwdDDDaassss
ccxxcxc#2222
dssSSSSddDDDD
D1Sqn2NYOHgTI
Hello
Alex
ssS@3

Ok, and let's say we want to delete all words from D1Sqn2NYOHgTI and back, this means
to delete the words (and the lines of them) :

alllllsadfsdasdf
qwdDDDaassss
ccxxcxc#2222
dssSSSSddDDDD
D1Sqn2NYOHgTI

trent.josephsen
July 23rd, 2010, 07:15 PM
$ sed -e '/D1Sqn2NYOHgTI/q'

'q' is a sed command that exits the script after printing the current pattern space.

hakermania
July 23rd, 2010, 07:36 PM
ok, thank you very much!

ghostdog74
July 24th, 2010, 03:56 AM
you said you want to delete from D1Sqn2NYOHgTI and back, so this is the sed command


$ cat file
alllllsadfsdasdf
qwdDDDaassss
ccxxcxc#2222
D1Sqn2NYOHgTI
dssSSSSddDDDD
Hello
Alex
ssS@3

$ sed -n '/D1Sqn2NYOHgTI/,$ { /D1Sqn2NYOHgTI/!p}' file
dssSSSSddDDDD
Hello
Alex
ssS@3



you can also use awk



$ awk '/D1Sqn2NYOHgTI/{f=1;next}f' file
dssSSSSddDDDD
Hello
Alex
ssS@3

piratesmack
July 24th, 2010, 08:57 PM
Here's what I came up with


$ awk '/^D1Sqn2NYOHgTI$/ {print NR + 1}' afile | xargs -I {} tail -n+{} afile
Hello
Alex
ssS@3


:oops:

Will have to read up on sed and awk