Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

scan

macrumors 6502
Original poster
Oct 24, 2005
344
0
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
 
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.
 
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:
Code:
sed -e 's/^[0-9][0-9]*.*$/numeric/g' <infile >outfile
 
scan said:
I don't knwo how to search for empty line
A regular expression for an empty line is /^$/ so s/^$/empty/g should work.

Personally I would do this in perl rather than sed.

B
 
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.

Code:
#!/usr/bin/awk -f

#----------------------------------------------------------
#
# %Z%%M% %I% %G%
#
# foo
#
# sxz 2/10/06
#
#----------------------------------------------------------

/[0-9*]/ {
        print "numeric"
}

/[a-zA-Z*]/ {
        print "word"
}

/^$/ {
        print "empty"
}
 
zimv20 said:
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.

Code:
#!/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
 
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
 
fisty said:
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
 
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?
 
scan said:
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:
Code:
ls -l | cut -d" " -f1
Example with columns:
Code:
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.,
Code:
ls -l | awk '{print $6,$7}'
 
Doctor Q said:
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:
Code:
ls -l | cut -d" " -f1
Example with columns:
Code:
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.,
Code:
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
 
scan said:
I have to use ls, tr, and cut. can't use awk. but I already tried those suggestions. they dont' really work
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:
Code:
ls -l | tr -s " " | cut -d" " -f5
The 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.
 
Doctor Q said:
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:
Code:
ls -l | tr -s " " | cut -d" " -f5
The 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
 
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 said:
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.
 
zimv20 said:
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?
Code:
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?
 
Doctor Q said:
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.
 
zimv20 said:
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.
 
Doctor Q said:
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.