LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
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, 07:13 AM   #1
nagendrar
Member
 
Registered: Apr 2008
Location: HYD, INDIA.
Posts: 151

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, 07:45 AM   #2
jhwilliams
Senior Member
 
Registered: Apr 2007
Location: Portland, OR
Distribution: Debian, Android, LFS
Posts: 1,168

Rep: Reputation: 210Reputation: 210Reputation: 210
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 07:52 AM.
 
Old 11-10-2011, 08:28 AM   #3
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
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, 09:41 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,357

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


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



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


All times are GMT -5. The time now is 06:44 PM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration