You are not logged in.
Pages: 1
Topic closed
i thought accomplishing this could be as simple as a:
sed -n '/regex/-1!p'
Guess not.
Any Suggestions?
Offline
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.
[git] | [AURpkgs] | [arch-games]
Offline
yeah, use perl.
my $before;
while(<>) {
/<your regex>/ or print $before;
$before = $_;
}
print $before;
Offline
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
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
Offline
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
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
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
I think googling for TLDP's ABS guide would help you massively. I see a lot of very simple syntax mistakes.
[git] | [AURpkgs] | [arch-games]
Offline
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
Execute:
bash hi.txt > hi.out
Compare:
diff hi.out fileB.txt
Look at man diff, man cmp, man comm for alternatives.
Offline
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
does anybody know what im talking about? im having such a hard time with this..
Offline
Are you sure this is not homework....
http://spot.pcc.edu/~cdjones/200804/CS1 … /lab5.html
Offline
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
So, it is homework which means.... CLOSED.
Offline
Pages: 1
Topic closed