|
|
|
Get the last few words of a line?
|
Original Message
|
Name: nmcspadden
Date: June 27, 2006 at 16:20:38 Pacific
Subject: Get the last few words of a line?OS: Linux Debian 3CPU/Ram: Pentium III and 512 MB |
Comment: Given a phrase like this: WORD1 WORD2: WORD3 WORD4 WORD5 I 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
Report Offensive Message For Removal
|
|
Response Number 1
|
Name: nails
Date: June 27, 2006 at 16:47:31 Pacific
|
Reply: (edit)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) }
Report Offensive Follow Up For Removal
|
|
Response Number 2
|
Name: nmcspadden
Date: June 28, 2006 at 10:16:38 Pacific
|
Reply: (edit)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!
Report Offensive Follow Up For Removal
|
|
Response Number 3
|
Name: nails
Date: June 28, 2006 at 11:25:52 Pacific
|
Reply: (edit)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.
Report Offensive Follow Up For Removal
|
|
Response Number 4
|
Name: nmcspadden
Date: June 28, 2006 at 12:10:12 Pacific
|
Reply: (edit)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
Report Offensive Follow Up For Removal
|
|
Response Number 5
|
Name: nails
Date: June 28, 2006 at 12:25:47 Pacific
|
Reply: (edit)With the modern shells such as ksh and bash, this is how you do arithmetic: #!/bin/ksh LineCount=65 LineDifference=$((LineCount-5)) echo $LineDifferenc
Report Offensive Follow Up For Removal
|
|
Response Number 6
|
Name: ghostdog
Date: June 29, 2006 at 07:11:13 Pacific
|
Reply: (edit)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,word3 Its more readable compared to shell commands.
Report Offensive Follow Up For Removal
|
|
Response Number 7
|
Name: ghostdog
Date: June 29, 2006 at 07:34:20 Pacific
|
Reply: (edit)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)
Report Offensive Follow Up For Removal
|
|
Response Number 8
|
Name: lchi2000g
Date: June 29, 2006 at 07:49:34 Pacific
|
Reply: (edit)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
Report Offensive Follow Up For Removal
|
|
Response Number 9
|
Name: ghostdog
Date: June 29, 2006 at 08:51:03 Pacific
|
Reply: (edit)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:]
Report Offensive Follow Up For Removal
|
Use following form to reply to current message:
|
|

|