You are not logged in.

#1 2008-12-02 08:22:53

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

From Regex to Regex

sed -n '/regexp/,$p'

Was trying just having it twice..and a few other attempts but really none that worked hmm

Offline

#2 2008-12-02 12:04:06

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

Re: From Regex to Regex

So this is giving you trouble: sed -n '/regexp1/,/regexp2/p'?

I think some caveats are explained by the output of:

seq 1 30 > seq.txt
sed -n '/1/,/7/p' seq.txt
sed -n '/^1$/,/7/p' seq.txt
sed -n '/2/,/7/p' seq.txt

Offline

#3 2008-12-02 17:02:09

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

Re: From Regex to Regex

How would i delete this regex and ONLY the three lines that follow ?

sed -n '/---/!p'

And for this:

sed -n '/regexp1/,/regexp2/p'

How can not include regex2, but still be able to print to it.

Offline

#4 2008-12-02 17:11:58

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

Re: From Regex to Regex

How would i delete this regex and ONLY the three lines that follow ?

sed '/regex/,+3d'
sed -n '/regex/,+3!p'

How can not include regex2

I don't think you can do this with the same construct, so you would have to delete that line explicitly:
sed -n '/regexp1/,/regexp2/{/regexp2/d;p}'
or pipe it to grep -v 'regexp2'

Offline

#5 2008-12-02 23:38:58

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

Re: From Regex to Regex

Yay! So those worked..

Another problem that im facing is a double and triple occurence of what im cutting.

I have the file:

Source File  1: "~/myFile.txt"
   [ Total of 10 Lines ]
         Num   Len  Line
        -----  ----- ------
        00006   000:
        00009   071: find / -name ".bashrc" -exec grep -q "PS1=" '{}' \; -printf "%M %h%f\n"
        00010   000:
        00013 * 084: find /home -name ".bashrc" -printf "%h\n" 2>/dev/null | sed -r 's/.*\/([^\/]*$)/\1/'
        00014   000:
        00016   047: grep -e "^[^:\.]*:" /etc/passwd | cut -d':' -f1
        00017   000:
        00019   049: grep -E "^[^:]{11,}:" /etc/passwd | cut -d':' -f1
        00020   000:
        00023 * 085: find / -regextype posix-extended -regex ".*\/[^\/]{11,}$" -printf "%H%f\n"2>/dev/null


I need just the commands. So i'll try something easy and use:

cat hello.txt | sed -n '/regex/,$p' | sed -n '/#/!p' | cut -d':' -f2

Which does isolte the commands, but with some minor problems.

So i try:

cat hello.txt | sed -n '/regex/,$p' | sed -n '/#/!p' | sed -n '/:/{n;p};h'

Which gives me:

[ Total of 10 Lines ]
        00009   071: find / -name ".bashrc" -exec grep -q "PS1=" '{}' \; -printf "%M %h%f\n"
        00013 * 084: find /home -name ".bashrc" -printf "%h\n" 2>/dev/null | sed -r 's/.*\/([^\/]*$)/\1/'
        00016   047: grep -e "^[^:\.]*:" /etc/passwd | cut -d':' -f1
        00019   049: grep -E "^[^:]{11,}:" /etc/passwd | cut -d':' -f1
        00023 * 085: find / -regextype posix-extended -regex ".*\/[^\/]{11,}$" -printf "%H%f\n"2>/dev/null

So almost!

Offline

#6 2008-12-03 01:25:54

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

Re: From Regex to Regex

LOL

 sed -e s/[^:]*://

Offline

#7 2008-12-03 01:50:38

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

Re: From Regex to Regex

I don't understand some things in your post, so if the command below isn't right, please explain some things like why you're using those specific sed commands.

sed -n 's/^[^:]*: //p' hello.txt

EDIT: sorry I took a while to write this and didn't see your post above:

sed -e s/[^:]*://

Yep that's what I had in mind. Either works fine in this situation.

Last edited by Procyon (2008-12-03 01:53:31)

Offline

#8 2008-12-03 02:02:44

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

Re: From Regex to Regex

damn another problem...now how i would i remove these lines?



1  ---------------



     2  "~/myFile.txt"
     3    [2 line(s) extend beyond column 80]

     4           Num   Len  Line
     5          -----  ----- ------
     6


lol, i dont want these lines..

Offline

#9 2008-12-03 02:04:29

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

Re: From Regex to Regex

could i use the " sed -e s/[^:]*:// " again to print after the first occurence of the ':'?

Offline

#10 2008-12-03 02:09:55

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

Re: From Regex to Regex

#8, that's where you need the "sed -n ...p"

#9, what?

Offline

#11 2008-12-03 02:27:07

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

Re: From Regex to Regex

1  ---------------



     2  "~/myFile.txt"
     3    [2 line(s) extend beyond column 80]

     4           Num   Len  Line
     5          -----  ----- ------
     6

For that, i guess i could just do:


 sed /--------/,+10d

hmm, that works..but i might need to change it.

Offline

#12 2008-12-03 02:31:46

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

Re: From Regex to Regex

What if you use this one:

sed -n 's/^[^:]*: //p'

If it doesn't contain a ": " it will not be printed.

Offline

#13 2008-12-03 02:36:48

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

Re: From Regex to Regex

Thanks for your help!!!

Offline

#14 2008-12-03 02:39:44

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

Re: From Regex to Regex

Ack, i do have one last question..

If i were to redirect these commands that i've isolated, how would i execute them 1 by one?

Offline

#15 2008-12-03 02:59:48

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

Re: From Regex to Regex

What do you mean by that?

Offline

#16 2008-12-03 03:12:33

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

Re: From Regex to Regex

FileA 

        00009   071: find / -name ".bashrc" -exec grep -q "PS1=" '{}' \; -printf "%M %h%f\n"
        00013 * 084: find /home -name ".bashrc" -printf "%h\n" 2>/dev/null | sed -r 's/.*\/([^\/]*$)/\1/'
        00016   047: grep -e "^[^:\.]*:" /etc/passwd | cut -d':' -f1
        00019   049: grep -E "^[^:]{11,}:" /etc/passwd | cut -d':' -f1
        00023 * 085: find / -regextype posix-extended -regex ".*\/[^\/]{11,}$" -printf "%H%f\n"2>/dev/null

(For the sake of the example: ) FileB

 00009   071: find / -name ".bashrc" -exec grep -q "PS1=" '{}' \; -printf "%M %h%f\n"
        00013 * 084: find /home -name ".bashrc" -printf "%h\n" 2>/dev/null | sed -r 's/.*\/([^\/]*$)/\1/'
        00016   047: grep -e "^[^:\.]*:" /etc/passwd | cut -d':' -f1
        00019   049: grep -E "^[^:]{11,}:" /etc/passwd | cut -d':' -f1
        00023 * 085: find / -regextype posix-extended -regex ".*\/[^\/]{11,}$" -printf "%H%f\n"2>/dev/null

i removed the line numbers from these commands, and redirected the result to a text file...Is there a way to execute these commands one by one? And ( what im thinking is to ) redirect that output to another text file and then compare them.

diff a b would be what i would use. But is there an argument to display the differences in the output ? Or just something to display a difference outside of the file..as its output.

FileA commands goto FileA
..

then execute the commands one by one and output differences between them.

Last edited by oxoxo (2008-12-03 03:14:26)

Offline

#17 2008-12-03 03:21:41

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

Re: From Regex to Regex

You mean something like

#! /bin/bash
#requires two filenames as argument:
[ $# -ne 2 ] && exit
[ ! -f "$1" -o ! -f "$2" ] && exit

sed -n 's/^[^:]*: //p' "$1" > "$1".edit
sed -n 's/^[^:]*: //p' "$2" > "$2".edit

diff "$1".edit "$2".edit

No idea what you would need to use for diff though. Look through man diff for tips.

--EDIT--

FileA commands goto FileA
..

then execute the commands one by one and output differences between them.

So not like the above? But actually run the commands in the file?

Last edited by Procyon (2008-12-03 03:25:31)

Offline

#18 2008-12-03 03:36:32

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

Re: From Regex to Regex

So not like the above? But actually run the commands in the file?

Yeah, like run the fileA save the output, then run command fileB and then save that output, and finally compare output to check for differences ( in the output ).

Offline

#19 2008-12-03 03:49:22

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

Re: From Regex to Regex

I think with a for loop and eval. I am not sure how eval messes up some characters like backslashes though. I tried it and seems to work:

#! /bin/bash
#requires two filenames as argument:
[ $# -ne 2 ] && exit
[ ! -f "$1" -o ! -f "$2" ] && exit

IFS="
"

for command in $(sed -n 's/^[^:]*: //p' "$1"); do
eval "$command"
done > "$1".cmd

for command in $(sed -n 's/^[^:]*: //p' "$2"); do
eval "$command"
done > "$2".cmd

diff "$1".cmd "$2".cmd

Offline

#20 2008-12-03 05:58:07

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

Re: From Regex to Regex

I cant get that to work, and havent heard too much about IFS..let me start over real fast..

I have a file that i broke up into two parts.

These Commands:

cat myfile.txt | sed -n '/regex/,/regex/{/regex/d;p}' | sed -n '/---/,+3!p' | sed -n '/#/!p' | sed 's/^[ ]*//' | sed -e s/[^:]*://

Gives me the first part of the file.

And These Commands:

cat myFile.txt | sed -n '/regex/,/regex/{/regex/d;p}' | sed -n '/#/!p' | sed -e s/[^:]*:// | sed /--------/,+10d

Basically spliting the file up into two parts.

Now..Finally...I can redirect to a text file. So do a chmod +X myfile.txt so i can execute it.
But wait... I need to execute them one by one..and compare the output..

So yeah, scripting this would be the way to go..Your right i would have two arguments. But when i use my.txt my2.txt nothing happens.

Again i need to execute each line of commands one by one.. The File looks like this:

find /home -name ".bashrc" -printf "%h\n" 2>/dev/null | sed -r 's/.*\/([^\/]*$)/\1/'

 grep -e "^[^:\.]*:" /etc/passwd | cut -d':' -f1

 grep -E "^[^:]{11,}:" /etc/passwd | cut -d':' -f1

 find / -regextype posix-extended -regex ".*\/[^\/]{11,}$" -printf "%H%f\n"2>/dev/null

And FileB:

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,}$'


 cat /etc/passwd | cut -d':' -f1 | egrep '[A-Za-z]{10,}$'

 du / -b 2>/dev/null | egrep '^.*/[^/]{10,}$' | head -20

I've been trying to figure out how i can execute a line from a each file upon specification of $1 and $2 and then compare them.

Last edited by oxoxo (2008-12-03 06:03:45)

Offline

#21 2008-12-03 12:02:50

Profjim
Member
From: NYC
Registered: 2008-03-24
Posts: 658

Re: From Regex to Regex

line_number=0
while read line_from_a; do
    ((line_number++))

    # there's got to be a better way of doing this...
    sed -n "${line_number}{p,q}" $file_b
    
    escape_any_quotes_etc_and_exec "$line_from_a" > "a_out.${line_number}"
    escape_any_quotes_etc_and_exec "$line_from_b" > "b_out.${line_number}"
    
    # alternatively, if you only need the diffs
    # diff <(escape_any_quotes_etc_and_exec "$line_from_a") <(escape_any_quotes_etc_and_exec "$line_from_b") > "diffs.${line_number}"

done < $file_a

We also need the escape_any_quotes_etc_and_exec function. If your data is simple, you won't need to do much.
You can look into lessecho (part of the less package), but I can't get it to do what I want. So I use this instead:

function quote {
    echo -n "'"
    echo -n "$1" | sed "s/'/'\\\\''/g"
    echo -n "'"
}

You can define escape_any_quotes_etc_and_exec as:

function escape_any_quotes_etc_and_exec {
    exec $(quote "$1")
}

Offline

#22 2008-12-03 13:26:58

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

Re: From Regex to Regex

If you already got the file to look like that then what happens if you just do bash FileA

And

bash FileA > FileA.output
bash FileB > FileB.output
diff FileA.output FileB.output

Offline

Board footer

Powered by FluxBB