LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
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-04-2006, 04:44 PM   #1
bullfrog1870
LQ Newbie
 
Registered: Nov 2006
Posts: 14

Rep: Reputation: 0
BASH Scripting - printing select lines in a file


Hello:

How do I print select lines in a file? I want to cycle through each line in a file, search for a digit in the 4th position, if greater than 3, then print the entire line.

Before:
12 5 67 345
1 2356 34
2 43222 12 444
233145633

After:
12 5 67 345
2 43222 12 444

It only printed these two lines since the character in the 4th position was greater than 3.

Thank you!
 
Old 11-04-2006, 04:48 PM   #2
bullfrog1870
LQ Newbie
 
Registered: Nov 2006
Posts: 14

Original Poster
Rep: Reputation: 0
Ok, that didn't display very well. I was trying to point out the fact that spaces are randomly interspersed throughout. Hopefully you get the point w/o the example.
 
Old 11-04-2006, 05:38 PM   #3
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 56
Did you mean something like this?
Code:
#!/bin/bash

cat file.txt | \
while read line ; do
set -- $line
if [[ $4 > 3 ]] ; then
   echo $line
fi
done

Last edited by homey; 11-04-2006 at 05:44 PM.
 
Old 11-04-2006, 05:57 PM   #4
bullfrog1870
LQ Newbie
 
Registered: Nov 2006
Posts: 14

Original Poster
Rep: Reputation: 0
homey:

It appears to be acting on the fourth word rather than the fourth character. Does that make sense?
 
Old 11-04-2006, 06:20 PM   #5
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 63
Perl is a little more suited to this sort of thing. You can do it from the command line here:
Code:
perl -n -e 'print if ( substr($_,3,1) > 3 );' file.txt
I'm sure someone can come up with a similarly elegant, readable and convenient python version...</challenge>

Last edited by matthewg42; 11-04-2006 at 06:23 PM.
 
Old 11-04-2006, 08:21 PM   #6
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 56
How about this?
Code:
#!/bin/bash

cat file.txt | \
while read line ; do
val=${line:3:1}

if [[ $val > 3 ]] ; then
   echo $line
fi
done
 
Old 11-04-2006, 08:47 PM   #7
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 63
Quote:
Originally Posted by homey
How about this?
Code:
#!/bin/bash

cat file.txt | \
while read line ; do
val=${line:3:1}

if [[ $val > 3 ]] ; then
   echo $line
fi
done
What about lines where column 4 is non-numeric (e.g. alpha character). Doesn't behave very well in these cases (prints lines where column 4 = alpha, e.g. "h".
 
Old 11-04-2006, 09:24 PM   #8
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 56
You're right, perhaps this will work better. The original example didn't have alpha in it.
Code:
#!/bin/bash

cat file.txt | \
while read line ; do
val=${line:3:1}

if [[ $val -gt 3 ]] ; then
   echo $line
fi
done
 
Old 11-05-2006, 04:29 AM   #9
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 63
I thought that would result in error messages when $val was non-numeric, but it doesn't when we use double [[ ]] around the condition.

Another day, another thing learned.
 
Old 11-05-2006, 08:50 AM   #10
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,245
Blog Entries: 16

Rep: Reputation: 233Reputation: 233Reputation: 233
hello homey. perhaps we should use cases instead:
Code:
#!/bin/bash

cat file.txt | \
while read line ; do
val=${line:3:1}

case ${val} in
[4-9])
	echo "${line}"
	;;
esac
done
just an alternative.

Last edited by konsolebox; 11-05-2006 at 08:55 AM.
 
Old 11-05-2006, 09:09 AM   #11
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 56
Thanks, that seems to work also.

Adding to my collection of notes .....

Edit: Actually, your code pointed out an error in mine. Where the file has many spaces, the echo statement needs quotes to handle spaces... echo "$line"
Code:
#!/bin/bash

cat file.txt | \
while read line ; do
val=${line:3:1}

if [[ $val -gt 3 ]] ; then
   echo "$line"
fi
done

Last edited by homey; 11-05-2006 at 09:18 AM.
 
Old 11-05-2006, 09:40 AM   #12
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,245
Blog Entries: 16

Rep: Reputation: 233Reputation: 233Reputation: 233
Quote:
Originally Posted by homey
Thanks, that seems to work also.

Adding to my collection of notes .....
you're welcome. come to think about the output
Code:
Before:
12 5 67 345
1 2356 34
2 43222 12 444
233145633

After:
12 5 67 345
2 43222 12 444
doesn't bullfrog1870 mean greater than or equal to 3 and not just greater than 3?
 
Old 11-05-2006, 10:06 AM   #13
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 56
Quote:
doesn't bullfrog1870 mean greater than or equal to 3 and not just greater than 3?
Don't know, the OP is kindof missing from this post which probably means his homework is already done.

Anyway, a person could use -ge if they want to include 3
 
Old 11-05-2006, 11:16 AM   #14
soggycornflake
Member
 
Registered: May 2006
Location: England
Distribution: Slackware 10.2, Slamd64
Posts: 249

Rep: Reputation: 31
Code:
sed -n '/^...[4-9]/p' <file>
 
Old 11-08-2006, 10:10 AM   #15
muha
Member
 
Registered: Nov 2005
Distribution: xubuntu, grml
Posts: 451

Rep: Reputation: 37
What's up with: cat file.txt | \
Why not use:
Code:
#!/bin/bash

while read line ; do
val=${line:3:1}

if [[ $val -gt 3 ]] ; then
   echo "$line"
fi
done < file.txt
I thought that was the normal syntax ....
 
  


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
Printing random lines of a file jrdioko Linux - Software 3 07-20-2006 10:00 AM
Inserting lines into a file through shell scripting false-hopes Linux - General 1 10-22-2005 12:39 PM
[bash] removing or editing lines in file Erhnam Programming 12 03-15-2005 08:25 AM
Can't get lines of a file with a Bash script.. barisdemiray Programming 2 08-11-2004 01:42 PM
[bash] remove lines from a file Drimo Programming 3 03-20-2004 12:16 PM


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