View Full Version : unix help (sed command)
scan
Feb 9, 2006, 04:20 PM
I am using the sed command to search a file for a particular pattern and replacing it with somehting depending on the pattern. what I'm having trouble with is replacing the whole line with the word and getting rid of the rest of the line. I don't want to manipulate the file though. just the output
for example:
12345blahblahblah
abcde
would be:
numeric
empty
word
but my expression does:
numericblahblahblah
Edit: my empty just doesn'st work. I don't knwo how to search for empty line
mklos
Feb 9, 2006, 06:28 PM
Did you try looking at the man pages in UNIX?
Just do:
man sed
and that will display the man (manual) pages for that command in UNIX.
Doctor Q
Feb 9, 2006, 08:10 PM
I suspect that you need to make the pattern match the whole line, so that the whole line is what gets replaced when the pattern is matched. So instead of matching numerics and replacing them with the word "numeric", you want to match numerics-followed-by-anything and replace THAT with the word "numeric".
Example:sed -e 's/^[0-9][0-9]*.*$/numeric/g' <infile >outfile
balamw
Feb 9, 2006, 08:19 PM
I don't knwo how to search for empty lineA regular expression for an empty line is /^$/ so s/^$/empty/g should work.
Personally I would do this in perl rather than sed.
B
Cooknn
Feb 9, 2006, 09:35 PM
sed -e 's/^[0-9][0-9]*.*$/numeric/g' <infile >outfileThat brings back memories :p
Doctor Q
Feb 9, 2006, 11:26 PM
You could also use awk and some if/then/else logic.
zimv20
Feb 10, 2006, 12:16 AM
ahhh, sed and awk. i love me some sed and awk.
scan -- i don't think you need sed at all. to me, this is an awk program.
#!/usr/bin/awk -f
#----------------------------------------------------------
#
# %Z%%M% %I% %G%
#
# foo
#
# sxz 2/10/06
#
#----------------------------------------------------------
/[0-9*]/ {
print "numeric"
}
/[a-zA-Z*]/ {
print "word"
}
/^$/ {
print "empty"
}
superbovine
Feb 10, 2006, 01:49 AM
ahhh, sed and awk. i love me some sed and awk.
scan -- i don't think you need sed at all. to me, this is an awk program.
#!/usr/bin/awk -f
#----------------------------------------------------------
#
# %Z%%M% %I% %G%
#
# foo
#
# sxz 2/10/06
#
#----------------------------------------------------------
/[0-9*]/ {
print "numeric"
}
/[a-zA-Z*]/ {
print "word"
}
/^$/ {
print "empty"
}
Doctor Q looks cooler! :p
zimv20
Feb 10, 2006, 01:59 AM
Doctor Q looks cooler! :p
what? elegance! simplicity! built-in recursion!
::shakes fist::
:)
fisty
Feb 10, 2006, 11:18 AM
i dont understand why if ur on a unix course u come to this site to cheat tbh...
wotn help ya....just give ya easy anwsers...
whatever
scan
Feb 10, 2006, 06:13 PM
i dont understand why if ur on a unix course u come to this site to cheat tbh...
wotn help ya....just give ya easy anwsers...
whatever
actually I've been trying to figure this out for days now and I finally figureded it out BY MYSELF. I don't come on here to "cheat" so you say. I completely forgot I asked for help on here and eventually figured it out. I'm not one of those people who try to get things done by cheating. It wouldn't benefit me because I actually have to learn this stuff. its not just for marks
oh yeah about thread topic, I know I dont' have to use sed but thats what the question asks for. infact I was discussing with my teacher at the question and she didn't knwo either. lol she kept suggesting solutions that wouldn't have worked. finally i figured it out and she copied down my answer
scan
Feb 10, 2006, 06:22 PM
this reminds me that I have another question. I'm trying to list the bytes of the files/dir in a directory. I'm piping ls and cut but I'm having trouble cutting the right field. any suggestions?
Doctor Q
Feb 10, 2006, 06:32 PM
this reminds me that I have another question. I'm trying to list the bytes of the files/dir in a directory. I'm piping ls and cut but I'm having trouble cutting the right field. any suggestions?Cut can cut either by fields (with -f) or by columns (with -c). You can use either with the output of ls. When you use fields, you need to consider what delimiter will be used and fields with variables amount of whitespace become a problem, but the first column is easy if you use " " (a space) as delimiter:ls -l | cut -d" " -f1Example with columns:ls -l | cut -c26-36
To avoid the whitespace problem, you can switch from cut to awk, which counts fields between whitespace as humans do, e.g.,ls -l | awk '{print $6,$7}'
scan
Feb 10, 2006, 06:34 PM
Cut can cut either by fields (with -f) or by columns (with -c). You can use either with the output of ls. When you use fields, you need to consider what delimiter will be used and fields with variables amount of whitespace become a problem, but the first column is easy if you use " " (a space) as delimiter:ls -l | cut -d" " -f1Example with columns:ls -l | cut -c26-36
To avoid the whitespace problem, you can switch from cut to awk, which counts fields between whitespace as humans do, e.g.,ls -l | awk '{print $6,$7}'
I have to use ls, tr, and cut. can't use awk. but I already tried those suggestions. they dont' really work
Doctor Q
Feb 10, 2006, 07:07 PM
I have to use ls, tr, and cut. can't use awk. but I already tried those suggestions. they dont' really workI don't understand why the cut -c example wouldn't get you the filesizes (the output you want, if I understand you correctly), assuming your "ls" command output has the filesize in columns 26 thru 36. If not, you can adjust the column numbers.
If you are supposed to use "tr", then the person posing the problem probably has something specific in mind. I suspect it is this:ls -l | tr -s " " | cut -d" " -f5The tr command with the -s switch means to translate multiple spaces into one space, which takes care of the whitespace problem I referred to above. Then you can use cut -f with space as the field delimiter.
For the record: The only case I know where this will fail is when the groupname is very long and/or the filesize is very big, such that those two fields run together with no intervening space, or similarly if the username and groupname run together. But I'm sure that's not a concern for your purposes.
scan
Feb 10, 2006, 07:17 PM
I don't understand why the cut -c example wouldn't get you the filesizes (the output you want, if I understand you correctly), assuming your "ls" command output has the filesize in columns 26 thru 36. If not, you can adjust the column numbers.
If you are supposed to use "tr", then the person posing the problem probably has something specific in mind. I suspect it is this:ls -l | tr -s " " | cut -d" " -f5The tr command with the -s switch means to translate multiple spaces into one space, which takes care of the whitespace problem I referred to above. Then you can use cut -f with space as the field delimiter.
For the record: The only case I know where this will fail is when the groupname is very long and/or the filesize is very big, such that those two fields run together with no intervening space, or similarly if the username and groupname run together. But I'm sure that's not a concern for your purposes.
thanks. i just figured it out. i was so happy i was dancing
zimv20
Feb 10, 2006, 11:45 PM
i'm a fan of cut, but sometimes it just can't extract the information i want. in that case, i use xargs.
Doctor Q
Feb 13, 2006, 02:09 PM
i'm a fan of cut, but sometimes it just can't extract the information i want. in that case, i use xargs.Care to give an example?
zimv20
Feb 13, 2006, 04:16 PM
Care to give an example?
i'm struggling to think of a good xargs example. the project i'm thinking of was in the early-mid 90's, and i wasn't allowed to take any code with me that i could now reference.
but, i will illustrate an example of where cut is deficient (you already demonstrated how to use awk to get around it). the instances where i used xargs were similar, though more complex (where i was processing the extracted data).
the example is grabbing block sizes from a directory:
% ls -ls
total 9048
1520 -rwxr-xr-x 1 zim staff 713656 May 9 2001 102-0300_IMG.JPG*
1416 -rwxr-xr-x 1 zim staff 661821 May 9 2001 103-0301_IMG.JPG*
992 -rwxr-xr-x 1 zim staff 457411 May 9 2001 103-0302_IMG.JPG*
1024 -rwxr-xr-x 1 zim staff 470563 May 9 2001 103-0303_IMG.JPG*
1400 -rwxr-xr-x 1 zim staff 651717 May 9 2001 103-0304_IMG.JPG*
1128 -rwxr-xr-x 1 zim staff 514108 May 9 2001 103-0305_IMG.JPG*
1568 -rwxr-xr-x 1 zim staff 737366 May 9 2001 103-0306_IMG.JPG*
%
because the blocksizes are of different lengths, some lines are left with padded white space (notice i subbed in -1 for -l to avoid the "total" line):
% ls -1s | cut -f 1 -d " "
1520
1416
1024
1400
1128
1568
%
as you showed above, awk solves this:
% ls -1s | awk '{print $1}'
1520
1416
992
1024
1400
1128
1568
%
Doc Q -- if you do know how to solve the example above using cut, please post it.
Doctor Q
Feb 13, 2006, 05:50 PM
1520 -rwxr-xr-x 1 zim staff 713656 May 9 2001 102-0300_IMG.JPG*
1416 -rwxr-xr-x 1 zim staff 661821 May 9 2001 103-0301_IMG.JPG*
992 -rwxr-xr-x 1 zim staff 457411 May 9 2001 103-0302_IMG.JPG*
1024 -rwxr-xr-x 1 zim staff 470563 May 9 2001 103-0303_IMG.JPG*
1400 -rwxr-xr-x 1 zim staff 651717 May 9 2001 103-0304_IMG.JPG*
1128 -rwxr-xr-x 1 zim staff 514108 May 9 2001 103-0305_IMG.JPG*
1568 -rwxr-xr-x 1 zim staff 737366 May 9 2001 103-0306_IMG.JPG*
Did you mean to show it with a leading space, like this?1520 -rwxr-xr-x 1 zim staff 713656 May 9 2001 102-0300_IMG.JPG*
1416 -rwxr-xr-x 1 zim staff 661821 May 9 2001 103-0301_IMG.JPG*
992 -rwxr-xr-x 1 zim staff 457411 May 9 2001 103-0302_IMG.JPG*
1024 -rwxr-xr-x 1 zim staff 470563 May 9 2001 103-0303_IMG.JPG*
1400 -rwxr-xr-x 1 zim staff 651717 May 9 2001 103-0304_IMG.JPG*
1128 -rwxr-xr-x 1 zim staff 514108 May 9 2001 103-0305_IMG.JPG*
1568 -rwxr-xr-x 1 zim staff 737366 May 9 2001 103-0306_IMG.JPG*In other words, are we talking about a case where the sizes in the first column are right-justified?
zimv20
Feb 13, 2006, 06:35 PM
Did you mean to show it with a leading space, like this? [...] In other words, are we talking about a case where the sizes in the first column are right-justified?
yes and yes.
Doctor Q
Feb 13, 2006, 08:22 PM
yes and yes.Cut -c isn't ideal for this output. Although a particular display will have the sizes in a fixed range of columns, you don't know the width since it depends on the longest filesize. And the spaces would confuse cut if you used cut -f.
What I didn't understand is how xargs would help.
zimv20
Feb 13, 2006, 11:53 PM
What I didn't understand is how xargs would help.
clearly, awk is the more straightforward step after cut. what i was remembering (from a project about 15 years ago), was encountering some issue w/ cut and using xargs as a workaround, but i can't recall what it was i was trying to do (and it must have been more involved than just grabbing a column of info).
so -- probably best to ignore my xargs comment. sorry for any confusion.
vBulletin® v3.8.6, Copyright ©2000-2012, Jelsoft Enterprises Ltd.