You are not logged in.

#1 2008-12-05 03:14:15

oxoxo
Member
Registered: 2008-10-31
Posts: 66

delete line before regex

i thought accomplishing this could be as simple as a: 

sed -n '/regex/-1!p'


Guess not.
Any Suggestions?

Offline

#2 2008-12-05 03:21:42

Daenyth
Forum Fellow
From: Boston, MA
Registered: 2008-02-24
Posts: 1,244

Re: delete line before regex

I don't know how I would do that in sed, but you could do it in perl pretty easily. Store the input in an array with one entry per line, and as you receive lines, match against the regex. When you get a match, act on the value of the previous array index.

Offline

#3 2008-12-05 04:27:15

peets
Member
From: Montreal
Registered: 2007-01-11
Posts: 936
Website

Re: delete line before regex

yeah, use perl.

my $before;
while(<>) {
  /<your regex>/ or print $before;
  $before = $_;
}
print $before;

Offline

#4 2008-12-05 06:05:56

oxoxo
Member
Registered: 2008-10-31
Posts: 66

Re: delete line before regex

ah, crap...This one wow....Is there a way to double space the output of this code and then execute each line separetly, storing that output into a varaible? ??
Hopefully, when i double space the output, every 'command line' will have its own line so that i can just use a loop throughout the file and execute each line seperately

#!/bin/bash

FILE="$1"

echo "You Entered $FILE"

if [ -f $FILE ]; then

tmp=$(cat $FILE | sed '/./!d' | sed -n '/regex/,/regex/{/regex/d;p}'| sed -n '/---/,+2!p' | sed -n '/#/!p' | sed 's/^[ ]*//' | sed -e\
s/[^:]*:// | sed -n '/regex /!p' | sed -n '/regex /!p' | sed -n '/"regex"/,+1!p' | sed -n '/======/!p' | sed -n '/regex/!p' | sed -n '/regex\
\r/!p' | sed -n '/regex /!p' )

fi

MyVar=$(echo $tmp)

echo "$MyVar | sed G"

Any Help is Very Much Appreciated.

Last edited by oxoxo (2008-12-05 06:06:54)

Offline

#5 2008-12-05 07:46:58

oxoxo
Member
Registered: 2008-10-31
Posts: 66

Re: delete line before regex

I've managed to double space the output but with a 'sed cant read error' How does one fix this error?

#!/bin/bash

FILE="$1"

echo "You Entered $FILE"

if [ -f $FILE ]; then

tmp=$(cat $FILE | sed '/./!d' | sed -n '/regex/,/regex/{/regex/d;p}'| sed -n '/---/,+2!p' | sed -n '/#/!p' | sed 's/^[ ]*//' | sed -e\
s/[^:]*:// | sed -n '/==> /!p' | sed -n '/--> /!p' | sed -n '/"regex"/,+1!p' | sed -n '/======/!p' | sed -n '//!p' | sed -n '/commands are fu\
n/!p' | sed -n '/Num /!p  | sed '/./=' $tmp | sed '/./N; s/\n/ /' )   <----- i guess that last sed does the trick.....So how to remove the error message?

fi
#MyVar=$(echo $tmp)

sed = "$tmp" | sed 'N; s'/\n/\' 2>/dev/null
#echo "$tmp"
#echo "$MyVar"
tmp=$(cat $FILE | sed '/./!d' | sed -n '/regex/,/regex/{/regex/d;p}'| sed -n '/---/,+2!p' | sed -n '/#/!p' | sed 's/^[ ]*//' | sed -e\
s/[^:]*:// | sed -n '/==> /!p' | sed -n '/--> /!p' | sed -n '/"regex"/,+1!p' | sed -n '/======/!p' | sed -n '//!p' | sed -n '/commands are fu\
n/!p' | sed -n '/Num /!p  | sed '/./=' $tmp | sed '/./N; s/\n/ /' )   <----- i guess that last sed works and numbers the lines.. But how to remove the error message?

Last edited by oxoxo (2008-12-05 07:58:09)

Offline

#6 2008-12-05 13:38:57

Daenyth
Forum Fellow
From: Boston, MA
Registered: 2008-02-24
Posts: 1,244

Re: delete line before regex

Holy crap that's a long pipeline...

Offline

#7 2008-12-05 13:41:00

creslin
Member
Registered: 2008-10-04
Posts: 241

Re: delete line before regex

Is this a homework assignment or something?  Why are you trying so hard to do this with bash?


ARCH|awesome3.0 powered by Pentium M 750 | 512MB DDR2-533 | Radeon X300 M
The journey is the reward.

Offline

#8 2008-12-05 13:45:00

Procyon
Member
Registered: 2008-05-07
Posts: 1,819

Re: delete line before regex

About problem 1, you can do it in sed, though this method removes blank lines (or creates them on deletion if you remove the s/^$//;t check) so you should use the Perl method.

sed -n '/regex/!{x;s/^$//;t;p};/regex/{p;s/.*//;x;d};${x;p}' seq.txt

For double spacing you can use:

sed '/^$/p' $FILE > newfile.txt

I think the read error is because in the stream you have one:
sed '/./=' $tmp

First of all, $tmp is a variable, and if you want sed to work on it you have to use
echo $tmp | sed '/./='

And second it is still in the $( ) loop so the variable doesn't exist yet, not to mention you are piping to it so all the data is lost (try piping something to a sed that works on a file)

You get the read error when $tmp contains garbage (I presume you ran it a few times before)

$ sed 's/e/d/' doesntexist
sed: can't read : No such file or directory

Offline

#9 2008-12-05 14:37:04

peets
Member
From: Montreal
Registered: 2007-01-11
Posts: 936
Website

Re: delete line before regex

tl;dr
with that many seds in a row, I'm pretty sure a single perl would be more efficient --and definetely more jovial.

$ perl -ple's/i1/o1/; s/i2/o2/; s/i3/o3/; ...' < my_input.file

will iterate over every line in 'my_input.file', making the substituions you specify and printing the result.
If you need to be doing stuff this complex, learning perl is a wise investment. Plus it's the funnest of languages I know.

Offline

#10 2008-12-05 14:46:23

Daenyth
Forum Fellow
From: Boston, MA
Registered: 2008-02-24
Posts: 1,244

Re: delete line before regex

I think googling for TLDP's ABS guide would help you massively. I see a lot of very simple syntax mistakes.

Offline

#11 2008-12-05 17:13:31

oxoxo
Member
Registered: 2008-10-31
Posts: 66

Re: delete line before regex

I can redirect the commands i've isolated from the original file to a hi.txt file. My next step would be to execute that file in the script somehow.

FILE="$1"

echo "You Entered $FILE"

if [ -f $FILE ]; then

tmp=$(cat $FILE | sed '/./!d' | sed -n '/regex/,/regex/{/regex/d;p}'| sed -n '/---/,+2!p' | sed -n '/#/!p' | sed 's/^[ ]*//' | sed -e\
s/[^:]*:// | sed -n '/==> /!p' | sed -n '/--> /!p' | sed -n '/"lab3.cmd"/,+1!p'\
 | sed -n '/======/!p' | sed -n '/regex/!p' | sed -n '/commands are fun/\
!p' | sed -n '/regex/!p')

fi

MyVar=$(echo $tmp > hi.txt)
echo "$MyVar"
#Execute hi.txt
#Compare it with another file using diff hi.txt fileB.txt ( or some other comparison command )
#Display a general file difference. ( im thinking a difference in bytes )
#echo something if their is a difference how im comparing them, like the difference in bytes

Offline

#12 2008-12-05 18:17:11

Procyon
Member
Registered: 2008-05-07
Posts: 1,819

Re: delete line before regex

Execute:

bash hi.txt > hi.out

Compare:

diff hi.out fileB.txt

Look at man diff, man cmp, man comm for alternatives.

Offline

#13 2008-12-05 22:27:18

oxoxo
Member
Registered: 2008-10-31
Posts: 66

Re: delete line before regex

For double spacing you can use:
sed '/^$/p' $FILE > newfile.txt
sed '/^$/p' $FILE > newfile.txt

Having trouble implementing that. It would seem easier to execute the file if the commands were on their own separate lines.

So i redirect a bunch of junk:

find / -type f -name '.bashrc' -print -exec grep PS1 '{}' \; 2>/dev/null | xargs ls -l 2>/dev/null find /home -name '.bashrc' 2>/dev/null cat /etc/passwd | cut -d':' -f1 | grep -v "\." | sort cat /home/student/joseph.hodges/item4.txt | cut -d"/" -f4 | egrep '[A-Za-z]{10,}$' du / -b 2>/dev/null | egrep '^.*/[^/]{10,}$' | head -20

Well, i know thats not just one long command. So to me it would make sense to double space this file..
So maybe i can do that early in the code..like this:

if [ -f $FILE ]; then

sed '/^$/p' $FILE > newfile.txt

tmp=$(cat newfile.txt | sed '/./!d' ...

But that wont work..

What i've done is isolated commands in a text file and redirected those commands ( as a whole w/o dblspacing ) to hi.txt file
I want my hi.txt file to look like this once i've double spaced it:

find / -type f -name '.bashrc' -print -exec grep PS1 '{}' \; 2>/dev/null | xargs ls -l 2>/dev/null 
find /home -name '.bashrc' 2>/dev/null 
cat /etc/passwd | cut -d':' -f1 | grep -v "\." | sort 
cat ~/hi.txt | cut -d"/" -f4 | egrep '[A-Za-z]{10,}$' 
du / -b 2>/dev/null | egrep '^.*/[^/]{10,}$' | head -20

Once i've gotten to this point i would want to use a loop that can save each line and that executes the commands to compare the bytes.

idk..<cries>

Offline

#14 2008-12-06 00:47:13

oxoxo
Member
Registered: 2008-10-31
Posts: 66

Re: delete line before regex

does anybody know what im talking about? im having such a hard time with this..

Offline

#15 2008-12-06 02:02:38

Allan
Pacman
From: Brisbane, AU
Registered: 2007-06-09
Posts: 11,405
Website

Re: delete line before regex

Are you sure this is not homework....
http://spot.pcc.edu/~cdjones/200804/CS1 … /lab5.html

Offline

#16 2008-12-06 07:15:30

oxoxo
Member
Registered: 2008-10-31
Posts: 66

Re: delete line before regex

Well, regex really isnt in this 70 dollar book that i have, and i've googled just about as much as i can stand. And the students that i've spoke with cant seem to pull it off either...

Offline

#17 2008-12-06 08:45:33

Allan
Pacman
From: Brisbane, AU
Registered: 2007-06-09
Posts: 11,405
Website

Re: delete line before regex

So, it is homework which means.... CLOSED.

Offline

Board footer

Powered by FluxBB