LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 07-13-2006, 12:31 PM   #1
stevie_velvet
Member
 
Registered: Aug 2004
Location: Italia + UK
Distribution: RHEL 4 + Fedora FC5
Posts: 118

Rep: Reputation: 15
ssimple shell script to parse a file ~sed or awk


i have a file called myfile

A 1
B 2
C 3


i need to use sed or awk file take a variable (as a ext input), as a number & search each line for that number(pattern) & extracts the letter
for example, If I type 2, the result will be b

Caveat ~There is a TAB between the letters & numbers ~ not spaces, also there's no editng of myfile

whose good at scripting ?
 
Old 07-13-2006, 12:47 PM   #2
raskin
Senior Member
 
Registered: Sep 2005
Location: Russia
Distribution: NixOS (http://nixos.org)
Posts: 1,893

Rep: Reputation: 68
Do you really need to do this in pure sed? I guess you want

read;
grep "^[^ ] $REPLY$" <file | sed -e 's/ .*//'

where " " should be written as <Tab> and file should not contain tabs except one in a line.
 
Old 07-13-2006, 02:17 PM   #3
stevie_velvet
Member
 
Registered: Aug 2004
Location: Italia + UK
Distribution: RHEL 4 + Fedora FC5
Posts: 118

Original Poster
Rep: Reputation: 15
hi raskin

that looks good!
I guess i'm really need for it to be combined with awk
i.e.
| awk '{print $1}'

should be included

..just need a example to remind me of sed1

tq!!!!!!!!

s
 
Old 07-13-2006, 02:35 PM   #4
stevie_velvet
Member
 
Registered: Aug 2004
Location: Italia + UK
Distribution: RHEL 4 + Fedora FC5
Posts: 118

Original Poster
Rep: Reputation: 15
..note sure waht you mena ~as " " isn't visible in your example

also where would i used read?

laslty...doens't s/ used as a substitution ?
 
Old 07-13-2006, 06:12 PM   #5
spooon
Senior Member
 
Registered: Aug 2005
Posts: 1,755

Rep: Reputation: 47
how about
Code:
read REPLY
grep "$REPLY$" myfile | awk '{print $1}'
 
Old 07-13-2006, 06:48 PM   #6
jlinkels
Senior Member
 
Registered: Oct 2003
Location: Bonaire
Distribution: Debian Lenny/Squeeze/Wheezy/Sid
Posts: 3,986

Rep: Reputation: 477Reputation: 477Reputation: 477Reputation: 477Reputation: 477
That can be shorter

Code:
awk -v n=my_number '$0 ~ n {print $1}' myfile
jlinkels
 
Old 07-14-2006, 12:24 AM   #7
stevie_velvet
Member
 
Registered: Aug 2004
Location: Italia + UK
Distribution: RHEL 4 + Fedora FC5
Posts: 118

Original Poster
Rep: Reputation: 15
yep

i realise sed is overkill & its a question of GREP \ EGREP & AWK

i'll digest this

thanks!

s
 
Old 07-14-2006, 03:41 AM   #8
spirit receiver
Member
 
Registered: May 2006
Location: Frankfurt, Germany
Distribution: SUSE 10.2
Posts: 424

Rep: Reputation: 33
Actually, sed is more lightweight than awk (64K against 243K), so it's probably not appropriate to call sed overkill. The following command will do as well:
Code:
sed -nre "s/^(.*)\t$NUMBER\$/\1/p" myfile
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
sed and awk in shell script bondoq Linux - Newbie 14 07-27-2007 01:52 AM
Help with a script to edit text file (awk? sed?) rickh Linux - Newbie 8 04-21-2005 08:24 PM
I need to parse a word: awk or sed? mehesque Programming 5 07-27-2004 04:23 PM
Parse a filename with awk and sed chrisk5527 Programming 2 06-08-2004 06:13 PM
How to read ans parse MS word file using a Linux Shell script. Alek Linux - General 2 11-10-2003 02:07 PM


All times are GMT -5. The time now is 05:20 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration