LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
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 08-02-2010, 09:47 AM   #1
raefdhaon
LQ Newbie
 
Registered: Aug 2010
Posts: 6

Rep: Reputation: 0
print line if next line blank


Hi folks!

I've written a script to parse a file and print each line that ends with matching pattern, if the next line is blank. The pattern lines are the result of
md5sum $i|sed 's/\path\///g' so that only md5 and filename appear. Here's what I'm using.

Quote:
for fline in `sed -n '/.*\.ext$/p' file1`
do
if [ "`sed -n -e '/'"$fline"'/ {n; p;}' file1`" == "" ]
then
echo ""$fline" has no info" >>file2
fi
done
This results in;

{md5} has no info
{filename} has no info


Instead of;

{md5} {filename} has no info


for each line with matching pattern and followed by a blank line.


This does print every matching pattern line as one line.

sed -n '/\.ext$/p' file >file2


Obviously, it's something in the for routine breaking the line, but darned if I can figure it out. I would appreciate help.
 
Old 08-02-2010, 10:17 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,491

Rep: Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867
Do you have spaces in your input? Remember that the for loop will use the IFS variable for parsing and hence split on any white space, this includes
space, tab and new line.
 
Old 08-02-2010, 10:31 AM   #3
raefdhaon
LQ Newbie
 
Registered: Aug 2010
Posts: 6

Original Poster
Rep: Reputation: 0
Yes, there is a space in the line.

md5sum file results in {md5} {filename}

I'm about 2 weeks into using linux and am unfamiliar with IFS(and for for that matter), but I will remember that for splits lines on spaces. Any workaround?
 
Old 08-02-2010, 12:00 PM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,491

Rep: Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867
Have a look at while loops and use a read statement:
Code:
while read line
do
    <your stuff here>
done
I will leave it to you to find out how to get your data into the loop
 
Old 08-02-2010, 01:18 PM   #5
raefdhaon
LQ Newbie
 
Registered: Aug 2010
Posts: 6

Original Poster
Rep: Reputation: 0
Thanks grail. I will have a look at while loops.

I worked with what I have a bit more. It's not pretty. It's a bit slower than I'd hoped for. But this gives me the results I want.

Code:
for fline in `sed -n '/.*\.ext$/p' file1`
do
if [ "`sed -n -e '/'"$fline"'/ {n; p;}' file1`" == "" ]
then
echo "$fline" >>file2
fi
done
sed 's/\(.*\)\.ext/\\\1.ext/g' file2|sed -e :a -e '$!N;s/\n\\\(.*\)/ \1 has no info/;ta' -e 'P;D' >file3
Echo only $fline to file2 which gives me;

{md5}
{filename}
{md5}
{filename}

Append backslash to beginning of any line with a {filename} in file2, then append any line beginning with backslash to previous line, changing backslash to a space and adding " has no info" to {filename} with results going to file3. Result is;

{md5} {filename} has no info

Looking at it now, I probably could have shortened the appending of the backslash by a few characters.

sed 's/\(.*\.ext\)/\\\1/g' file2
 
  


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
Want to print fouth line after resulted line using grep with tail command saurabhmehan Linux - Newbie 10 08-04-2010 11:38 PM
awk command line: blank line record sep, new line field sep robertmarkbram Programming 4 02-21-2010 05:25 AM
grab the line below a blank line and the line above the next blank line awk or perl? Pantomime Linux - General 7 06-26-2008 08:13 AM
script to check the last line in a file is blank line or not naveensankineni Programming 10 03-01-2008 11:13 PM
how do I print 'blank space' to standard out in C on same line? purpleburple Programming 7 08-18-2002 11:54 PM


All times are GMT -5. The time now is 08:05 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration