Getting the first word using sed,awk or perl


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Getting the first word using sed,awk or perl
# 1  
Old 08-21-2010
Getting the first word using sed,awk or perl

Input:
Code:
root:x:0:
daemon:x:1:
bin:x:2:
sys:x:3:
adm:x:4:
tty:x:5:
disk:x:6:
lp:x:7:
mail:x:8:

Output:
Code:
root
daemon
bin
sys
adm
tty
disk
lp
mail

# 2  
Old 08-21-2010
Code:
sed "s/:.*//"

This User Gave Thanks to Scott For This Post:
# 3  
Old 08-21-2010
Code:
awk -F: '{print $1}' infile

This User Gave Thanks to kevintse For This Post:
# 4  
Old 08-22-2010
Shorter one Smilie
Code:
awk -F: '$0=$1' file

or
Code:
cut -d: -f1 file

This User Gave Thanks to danmero For This Post:
# 5  
Old 08-22-2010
Code:
perl -F: -lane 'print $F[0]'  infile

# 6  
Old 08-22-2010
Code:
# sed 's/\([^:]*\).*/\1/' infile
root
daemon
bin
sys
adm
tty
disk
lp
mail

# 7  
Old 08-23-2010
Code:
while IFS=":" read -r c1 c2
do
  echo "$c1"
done < myfile

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Finding a word with awk or sed

Hello, in a AIX system : AIX CDRATE01 2 7 00FAB3114C00 my following commande give the result : LISTE /tmp/RESS **************************************************************** Liste TYPE = XXXXXXX EX = YYYY VER ... (13 Replies)
Discussion started by: sam01
13 Replies

2. Shell Programming and Scripting

Retrieve information Text/Word from HTML code using awk/sed

awk/sed newbie here. I have a HTML file and from that file and I would like to retrieve a text word. <font face=arial size=-1><li><a href=/value_for_clients/Tokyo/abc_process.txt>abc</a> NDK Version: 4.0 </li> <font face=arial size=-1><li><a... (6 Replies)
Discussion started by: sk2code
6 Replies

3. Shell Programming and Scripting

perl lwp find word and print next word :)

hi all, I'm new there, I'm just playing with perl and lwp and I just successfully created a script for log in to a web site with post. I have a response but I would like to have something like this: I have in my response lines like: <div class="sender">mimi020</div> <some html code.....>... (3 Replies)
Discussion started by: vogueestylee
3 Replies

4. Shell Programming and Scripting

sed / awk to get specific word in line

I have http log that I want to get words after specific "tag", this a sample line from the log: 98,POST,200 OK,www.facebook.com,Mozilla/5.0 (Windows NT 6.1; WOW64; rv:9.0.1) Gecko/20100101 Firefox/9.0.1,/ajax/updatestatus.php?__a=1,datr=P_H1TgjTczCHxiGwdIF5tvpC; lu=Si1fMkcrU2SInpY8tk_7tAnw;... (6 Replies)
Discussion started by: erlanq
6 Replies

5. Shell Programming and Scripting

Extract word from text (sed,awk, etc...)

Hello, I need some help extracting the number after the RBA e.g 15911688 from the below block of text (e.g: grep RBA |sed .......). The code should be valid for blocks if text generated at different times as well and not for the below text only. ... (2 Replies)
Discussion started by: drbiloukos
2 Replies

6. Shell Programming and Scripting

making the first character of word using uppercase using awk and sed

I want to make the first character of some words to be uppercase. I have a file like the one below. uid,givenname,sn,cn,mail,telephonenumber mattj,matt,johnson,matt johnson,mattj@gmail.com markv,mark,vennet,matt s vennet,markv@gmail.com mikea,mike,austi,mike austin,mike@gmail.com I want... (3 Replies)
Discussion started by: matt12
3 Replies

7. Shell Programming and Scripting

sed or awk to print 2nd last word

Hi, I have a file which has the following /usr/new/xyz/abc /us1/neb/yxr/def /usr/bin/cloud1/fgh /net/bin1/txt1/kdq I want to do something like this /usr/new/xyz/abc xyz /us1/neb/yxr/def yxr /usr/bin/cloud1/fgh cloud1 /net/bin1/txt1/kdq txt1 I need to add the 2nd last word to the... (3 Replies)
Discussion started by: matbrow
3 Replies

8. Shell Programming and Scripting

awk or sed command to print specific string between word and blank space

My source is on each line 98.194.245.255 - - "GET /disp0201.php?poc=4060&roc=1&ps=R&ooc=13&mjv=6&mov=5&rel=5&bod=155&oxi=2&omj=5&ozn=1&dav=20&cd=&daz=&drc=&mo=&sid=&lang=EN&loc=JPN HTTP/1.1" 302 - "-" "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Trident/4.0; .NET CLR 1.0.3705; .NET CLR... (5 Replies)
Discussion started by: elamurugu
5 Replies

9. Shell Programming and Scripting

Ignore first word using sed in PERL

Please help me in ignoring first word in a line example Input log 123^Babd^Basdf789^B098^Bouiou Desired output abd,asdf789,098,ouiou 123 should be ignored is this possible using sed regular expressions Use code tags - you got a PM with a guide. (2 Replies)
Discussion started by: thankful123
2 Replies

10. Shell Programming and Scripting

Sed or awk cut all lines after word

Hi, sorry for newbie question :confused: can't find how to cut ? from 1000 2000 word some text1.... 100 200 300 word some text2.... 10 20 30 abc word some text3.... to some text1.... some text2.... some text3.... (7 Replies)
Discussion started by: Trump
7 Replies
Login or Register to Ask a Question