LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-10-2011, 06:13 AM   #1
nagendrar
Member
 
Registered: Apr 2008
Location: HYD, INDIA.
Posts: 154

Rep: Reputation: 15
get particular value from the given line using shell script


I have following two lines

Model: WDC WD400BB-75C Revision: Serial No: WD-WMA8H3231261 Size: 40.00GB <39999504384 bytes>


Vendor: Lite-On Product: LTN486 48x Max Revision: YD01 Serial No:1234

please help me to get values (Ex: WD-WMA8H3231261,1234) of "Serial No" only using shell script?

or

Please help me to cut until "Serial No" and get the remaining.


ThanQ,
Nagendra
 
Old 11-10-2011, 06:45 AM   #2
jhwilliams
Senior Member
 
Registered: Apr 2007
Location: Portland, OR
Distribution: Debian, Android, LFS
Posts: 1,168

Rep: Reputation: 211Reputation: 211Reputation: 211
Code:
sed 's/.*Serial No: \?\([A-Z0-9\-]*\).*/\1/' filename
Or, add a clause to get rid of the empty lines, too:

Code:
sed -e 's/.*Serial No: \?\([A-Z0-9\-]*\).*/\1/' -e '/^[ \t]*$/d' filename
outputs:
WD-WMA8H3231261
1234

Last edited by jhwilliams; 11-10-2011 at 06:52 AM.
 
Old 11-10-2011, 07:28 AM   #3
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
If "Serial No: <serial number>" is always followed by space or line end this pure bash solution
Code:
while read -r line
do
    buf=${line#*Serial No: }
    echo $(buf% *}
done < input.txt
 
Old 11-10-2011, 08:41 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Code:
awk '/Serial No/{getline;print}' RS=" *: *" file
 
  


Reply



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



Similar Threads
Thread Thread Starter Forum Replies Last Post
bash shell script read file line by line. Darren[UoW] Programming 57 04-17-2016 06:07 PM
how to replace line of file with another line using awk in shell script amit_pansuria Programming 3 03-29-2009 09:43 AM
How to identify a line and replace another string on that line using Shell script? Sid2007 Programming 10 10-01-2007 08:49 PM
shell script find a line and the next line (grep?) metalx1000 Programming 5 07-24-2007 08:41 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 08:17 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration