Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Given a phrase like this:
WORD1 WORD2: WORD3 WORD4 WORD5I just want WORD3 WORD4 WORD5. I can tail the file it
comes from to get the last line, but how do I get only
those last few words?It needs to be done autonomously (i.e. I can't do it
through vi or something like that- it's part of a script). I
thought I could use sed to replace the spaces with
newlines and then just get the lines I want, and swap 'em
back, but then I realized that sed can't substitute newlines
in, and getting 'em back out is no easy solution either.Any suggestions?
--
N. McSpadden

Two ways:
echo "WORD1 WORD2: WORD3 WORD4 WORD5"|cut -d " " -f3-
echo "WORD1 WORD2: WORD3 WORD4 WORD5"|awk ' {
e=NF-2
for(i=e; i<=NF; i++)
if(i < NF)
printf("%s ", $i)
else
printf("%s\n ", $i)}

Thanks! Using cut worked great.
One more question:
If I do a command that has results that look like this:
49 Mount Point: /A/Stuff
65 Mount Point: /How do I specifically grep for the line that has the mount
point of /? If I do a grep for "Mount Point: ", I get the
results above. However, I need to differentiate the root
volume from a secondary one, but I can't find a regular
expression that will allow me to do so.I thought this might work:
results | grep "/[\n]"
Using the regular expression for newline after the / so
that the line /A/stuff would be ignored, but it gives me an
empty result. (Assume that "results" is a command that
gives the output shown above).I've tried a number of different regular expressions, but
none of them seem to work. How do I distinguish the /
that is followed by nothing from the / that is followed by
by other stuff?Thanks!

How about searching for the string with a / at the end of the line:
grep 'Mount Point: /$' data.file
This works fine on my Solaris 7 box. If grep doesn't work for you, try egrep.

Wow, that was way easier. /sigh, time to go look over my
Regular Expressions guides again.One last question, since you've already been so helpful:
So let's say I have the line:65 Mount Point: /
Now I can use cut to get the line count at the beginning of
this input (the line is a result of a command | cat -n | grep
"Mount Point: /$"). Using cut, I can get the characters that
are the numbers:65
However, if I store this in a variable, it stores it literally,
like a string. If I want to use this for mathematical
operations (i.e. subtracting this from the total line count
given via wc -l), how can I do that? If I try:LineDifference = $LineCount (65) - 5
results in
echo $LineDifference = 65 -5
which doesn't do what I want at all. Any last suggestions?
--
N. McSpadden

With the modern shells such as ksh and bash, this is how you do arithmetic:
#!/bin/ksh
LineCount=65
LineDifference=$((LineCount-5))
echo $LineDifferenc

with python , it's even easier. Assuming a string "WORD1 WORD2: WORD3 WORD4 WORD5" and ":" separates them
theline = "WORD1 WORD2: WORD3 WORD4 WORD5"
last3words = theline.split(":")[1].strip()
word1,word2,word3 = last3words.split()
print word1,word2,word3Its more readable compared to shell commands.

f = open("yourfile")
for linenum, line in enumerate(f)):
number = line.split(" ")[0]
lastone = line.split(" ")[-1]
if lastone.strip() == "/":
thenumber = number
print "Line difference:%d" %(number-linenum)

I'm confused. Which one is simpler?
===> shell
echo "WORD1 WORD2: WORD3 WORD4 WORD5"|cut -d " " -f3-
===> python
theline = "WORD1 WORD2: WORD3 WORD4 WORD5"
last3words = theline.split(":")[1].strip()
word1,word2,word3 = last3words.split()
print word1,word2,word3
Luke Chi

if you are comparing like this, then the python script can be rewritten:
words = "WORD1 WORD2: WORD3 WORD4 WORD5"
word1,word2,word3 = words.split()[-3:]These 2 lines of code has already placed each word into variables for you .
if you just want to print out the last 3 words:words = "WORD1 WORD2: WORD3 WORD4 WORD5"
print words.split()[-3:]or :
"WORD1 WORD2: WORD3 WORD4 WORD5".split()[-3:]

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |