LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   BASH Scripting - printing select lines in a file (https://www.linuxquestions.org/questions/linux-newbie-8/bash-scripting-printing-select-lines-in-a-file-498645/)

bullfrog1870 11-04-2006 03:44 PM

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!

bullfrog1870 11-04-2006 03:48 PM

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.

homey 11-04-2006 04:38 PM

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


bullfrog1870 11-04-2006 04:57 PM

homey:

It appears to be acting on the fourth word rather than the fourth character. Does that make sense?

matthewg42 11-04-2006 05:20 PM

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>

homey 11-04-2006 07:21 PM

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


matthewg42 11-04-2006 07:47 PM

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".

homey 11-04-2006 08:24 PM

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


matthewg42 11-05-2006 03:29 AM

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. :)

konsolebox 11-05-2006 07:50 AM

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.

homey 11-05-2006 08:09 AM

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


konsolebox 11-05-2006 08:40 AM

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?

homey 11-05-2006 09:06 AM

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

soggycornflake 11-05-2006 10:16 AM

Code:

sed -n '/^...[4-9]/p' <file>

muha 11-08-2006 09:10 AM

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 ....

konsolebox 11-08-2006 09:31 AM

It's just up to you if you're up for speed. I find the first one a lot more common and readable though. You can't also use that normal syntax if you're going the use commands other than cat like sed or grep.

matthewg42 11-08-2006 09:33 AM

Shortest one so far I think...
Code:

grep '^...[3-9]' file.txt


All times are GMT -5. The time now is 01:53 PM.