Last word of lines


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Last word of lines
# 1  
Old 01-27-2005
Question Last word of lines

Hello,

I have a file in which there are many lines.
I don't know the length of any line and they also vary.
I want to pick the last word of those lines which end with for eg. xyz

Thanks in Advance
# 2  
Old 01-27-2005
Quote:
I want to pick the last word of those lines which end with for eg. xyz

i'm not sure what the above meant by ...

but , to get the last word of all lines in file
do the following

Code:
awk '{ print $NF }' file1

# 3  
Old 01-27-2005
If I understand the problem, I think it's more like this...
Code:
sed -e :a -e 's/\(.*\)\( .*xyz\)$/\1/;ta' -e's/.*xyz$//;s/.* //'

# 4  
Old 01-27-2005
Hello bhargav,

awk '{ print $NF }' file1
worked out
Thanks

Perderabo:

Your script gave me the last word of the last line, but i require the last word of each line ending with xyz.
and also i am quite novice to 'sed' programming and would request you to please explain that script to me.
# 5  
Old 01-27-2005
From google:


awk's basic mode of operation is to read its input, chop each line into fields separated by some delimiter (white space by default, but you can change it), and then allow you to do pattern matching and other operations based on those fields. The thing I use it for most often is to grab a particular field.

Let's look at a trusty long ls listing:


--------------------------------------------------------------------------------

% ls -l
-rw-r--r-- 1 jeffy 28 May 9 16:12 Makefile
-rwxr-xr-x 1 jeffy 24576 May 28 11:31 foo
-rw-r--r-- 1 jeffy 57 May 9 16:13 foo.c
-rw-r--r-- 1 jeffy 57 May 28 11:37 foobar
-rw-r--r-- 1 jeffy 71 Jun 2 11:45 fumpty


--------------------------------------------------------------------------------
Suppose I want to grab just the file sizes for some reason. awk numbers fields starting with 1 (not zero like you'd expect from a bunch of unix geeks), so we count across and see that we want to print out field 4, so just do this:
--------------------------------------------------------------------------------

% ls -l | awk '{print $4}'
28
24576
57
57
71


--------------------------------------------------------------------------------
Easy as pie. Notice that the awk program is enclosed in single quotes. This protects the "$4" from the shell so it gets evaluated by awk, not csh (or whatever)
You can print out multiple columns in any order by separating them with commas:


--------------------------------------------------------------------------------

% ls -l | awk '{print $3, $1, $4, $NF}'
jeffy -rw-r--r-- 28 Makefile
jeffy -rwxr-xr-x 24576 foo
jeffy -rw-r--r-- 57 foo.c
jeffy -rw-r--r-- 57 foobar
jeffy -rw-r--r-- 71 fumpty


--------------------------------------------------------------------------------
Notice that the separating white space is not preserved, but gets scrunched down to a single space.
Wait a minute, what's with that "$NF" in that last example? NF is an internal awk variable that always represents the Number of Fields in the current line. By sticking a dollar sign in front of it, I get the equivalent of a "$8" when I run the script on the "ls -l" output. But I don't have to know how many fields there are, I can just grab the last one.


-Thanks .--from your question - it made me learn "awk"!!
# 6  
Old 01-27-2005
One more thing: field seperator

awk -F:

the "-F" flag sets the field separator character as :

white space by default, separating white space is not preserved, but gets scrunched down to a single space.

hope this hekps!!
# 7  
Old 01-31-2005
Thanks,

In awk, we have a BEGIN and END construct.
Generally we place initialisations in BEGIN and final code in END.
( See i have started it also ).

My basic awk script is:

Begin {print ...} /regurlar-exp/ file End {print ...}

Its working fine.
But sometimes, the file which i am using as input becomes empty because its generated by some other script file.
Now only my End statements get printed, the Begin statements get bypassed.

Can somebody help me as to why is this happening and how can i solve it.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Find all lines in file such that each word on that line appears in at least n lines of the file

I have a file where every line includes four expressions with a caret in the middle (plus some other "words" or fields, always separated by spaces). I would like to extract from this file, all those lines such that each of the four expressions containing a caret appears in at least four different... (9 Replies)
Discussion started by: uncleMonty
9 Replies

2. Shell Programming and Scripting

Shell Script @ Find a key word and If the key word matches then replace next 7 lines only

Hi All, I have a XML file which is looks like as below. <<please see the attachment >> <?xml version="1.0" encoding="UTF-8"?> <esites> <esite> <name>XXX.com</name> <storeId>10001</storeId> <module> ... (4 Replies)
Discussion started by: Rajeev_hbk
4 Replies

3. UNIX for Dummies Questions & Answers

Delete lines with a word and their above lines

Hi, i have a file like this: A1 kdfjdljfdkljfdlf A2 lfjdlfkjddkjf A3 ***no hit*** A4 ldjfldjfdk A5 ***no hit*** A6 jldfjdlfjdlkfjd I want to remove the lines "***no hit*** and their above line to get an output file like this: (11 Replies)
Discussion started by: the_simpsons
11 Replies

4. UNIX for Dummies Questions & Answers

How to grep a word and the two lines before?

Hi I have this txt file getting from a lpstat command. XA40 XA40 0 Unknown 0 0 1 1 1 0 Unknown LPD 0 0 1 1 2 0 Unknown specified 0 0 1 1 3 XA99 @spip READY : (FATAL ERROR) 0781-233 Unknown host spiprs01.mon.local. XA01 @xs00 READY XA01 XA01 0 Unknown 0 0 1 1 1 0 Unknown LPD 0 0 1 1... (5 Replies)
Discussion started by: npatao71
5 Replies

5. Shell Programming and Scripting

Search the word to be deleted and delete lines above this word starting from P1 to P3

Hi, I have to search a word in a text file and then I have to delete lines above from the word searched . For eg suppose the file is like this: Records P1 10,23423432 ,77:1 ,234:2 P2 10,9089004 ,77:1 ,234:2 ,87:123 ,9898:2 P3 456456 P1 :123,456456546 P2 abc:324234 (2 Replies)
Discussion started by: vsachan
2 Replies

6. Shell Programming and Scripting

deleting lines above and below a word!

i jst want to delete a host entry from httpd.conf for eg: i have entries such as: <VirtualHost 192.168.1.157:80> DocumentRoot /home/karthik ServerName kar </VirtualHost> <VirtualHost 192.168.1.157:80> DocumentRoot /home/karthik1 ServerName www </VirtualHost> <VirtualHost... (4 Replies)
Discussion started by: jacky29
4 Replies

7. Shell Programming and Scripting

grep lines containing a word only twice.

Hi, I have a query on using grep options. I tried with several options but unable to do it. lanite:52> cat note 123 456 ab 123 ab cv 234 4566 67 ab gh tij ab 12 34 ab ab cv dfgv ab cv ab kjhk ab ghj sdf dfg ab jljklj ab Now, I need to use grep to find the line which contains the... (6 Replies)
Discussion started by: dilipr25
6 Replies

8. Shell Programming and Scripting

Word count of lines ending with certain word

Hi all, I am trying to write a command that can help me count the number of lines in the /etc/passwd file ending in bash. I have read through other threads but am yet to find one indicating how to locate a specifc word at the end of a line. I know i will need to use the wc command but when i... (8 Replies)
Discussion started by: warlock129
8 Replies

9. UNIX for Dummies Questions & Answers

append word to lines

Hi all, Can someone suggest how to append some word to all lines in file. for example word "Honey" to file f1 with lines: Mia Katrin Elizabeth to get Honey Mia Honey Katrin Honey Elizabeth Thanks in advance Givi (3 Replies)
Discussion started by: giviut
3 Replies
Login or Register to Ask a Question