Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

Sed help?

Name: Anonymous 2012-04-09 22:48

sup /prog/, I need your help for a bash script I'm making.
The last thing I need is a 3-step line.
>1. find the first instance of one less than an existing variable ($X - 1)
>2. search for the first instance of a string above that
>3. take the line of step 2 and save it as a variable

How could I do this using sed/grep?
Thanks.

Name: Anonymous 2012-04-09 23:43

This is impossible using sed or grep alone.  You might be able to hack together some abomination with awk and sh, but you'd probably be better off just using Perl.

Name: Anonymous 2012-04-10 0:02

>>2
You can do this with sed and expr or shell arithmetic.
instance of a string above that
If you mean the string is on the previous line, you would need to buffer the sed input with N and hold two lines at once. If a string can span multiple lines its slightly more difficult. You can put a flag in the hold space to determine whether you're in a string or not.
http://sed.sf.net/ has a lot of good resources.

Name: Anonymous 2012-04-10 0:30

OP here. I can do step 1 and 3 already, it's just step 2 that I'm having trouble with.

#step one
X="15"
Y=$(($X - 1))
Z=$(sed -n '/'$Y'/{=;q;}' file)

#step three
A=$(echo steptwo)

Name: Anonymous 2012-04-10 1:55

I did it /prog/.

X="15"
Y=$(($X - 1))
Z=$(sed -n '/lol/{=;q;}' file)
T=true

while [ $T == true ]; do
        Z=$(($Z - 1))
        A=$(sed $Z'q;d' file)
        if [ $A == "<item>" ]; then
                T=false
        fi
done
echo $Z

Name: bampu pantsu 2012-05-29 4:17

bampu pantsu

Don't change these.
Name: Email:
Entire Thread Thread List