LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 05-30-2013, 02:09 AM   #1
marcuka68
LQ Newbie
 
Registered: May 2013
Posts: 1

Rep: Reputation: Disabled
dmesg reading line by line with sed command


Hello, sorry if I not post at right forum!

I'm writing sh (not bash) script. Want to read dmesg line by line and examine each line content. To date I'm trying with sed command but no success. Follow part of the script which is problem to be realized by me:

while [ "$notSuccess" -eq 1 ]
do
usbProductId=`dmesg | sed -n '$i,$ip' | grep 'New USB device found' | cut -d '=' -f 3`
i=`expr $i + 1`
done


The problem is that the command "sed -n '$i,$ip'" isn't actualy replaced by "sed -n '1,1p'", "sed -n '2,2p'" and so on. How can I realize that?
 
Old 05-30-2013, 06:49 PM   #2
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,836
Blog Entries: 1

Rep: Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251
Try double quotes ("$i,$ip") for variable substitution.
 
Old 05-31-2013, 12:41 PM   #3
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Please use ***[code][/code]*** tags around your code and data, to preserve the original formatting and to improve readability. Do not use quote tags, bolding, colors, "start/end" lines, or other creative techniques. Thanks.

Scripting questions generally go in the programming forum. You can use the "report" button to ask the moderators to move it for you.

For better results, always post an example of the input you want to work on, and the output you desire from it. Explain clearly what you're trying to do, any be sure to provide other data necessary to replicate what you want. In this case, what do the $i and $ip variables hold and match?

Is the loop really necessary? There are much better options available if all you want is to get the first hit. expr is also completely superfluous in modern shells, since they all have integer arithmetic built in.

In any case, a single awk command will almost certainly do the job you want better than your chain of multiple grep/sed/cut commands.

Here are a few useful awk references:
http://www.grymoire.com/Unix/Awk.html
http://www.gnu.org/software/gawk/man...ode/index.html
http://www.pement.org/awk/awk1line.txt
http://www.catonmat.net/series/awk-one-liners-explained
 
  


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
[SOLVED] Cursor does not move to the bottom line and overwrite to same line on command line mesuutt Linux - Newbie 3 02-25-2012 08:04 AM
[SOLVED] Insert line using sed or awk at line using line number as variable sunilsagar Programming 11 02-03-2012 10:48 AM
Need help with sed command: if a line contains >2 colons (:) delete it and line above kmkocot Linux - Newbie 1 12-27-2011 08:51 AM
Programing Question: Reading file line by line.. then char by char in each line adriumroot Programming 8 05-30-2010 01:55 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 09:11 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