Read command stop on either EOF or a specific line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Read command stop on either EOF or a specific line
# 1  
Old 07-22-2014
Read command stop on either EOF or a specific line

I'm trying to stop reading a file until the end of the file is reached or a defined delimiter line is reached. Some how I need to test the fail state of the last 2 commands, not just the last command.

Code:
echo -e "hello\ngoodbye\n####\ntesting" | while read line; [[ "$line" != "####" ]]; do echo "$line"; done
hello
goodbye

However
Code:
echo -e "hello\ngoodbye\n####\ntesting" | while read line; [[ "$line" != "XXXX" ]]; do echo "$line"; do

keeps running expecting input.

I tried
Code:
echo -e "hello\ngoodbye\n####\ntesting" | while read line; [[ "$?" -ne 0 || "$line" != "XXXX" ]]; do echo "$line"; done

but it does not work.

Mike

Last edited by Michael Stora; 07-22-2014 at 04:51 PM..
# 2  
Old 07-22-2014
Try
Code:
echo -e "hello\ngoodbye\n####\ntesting" | while read line && [ "$line" != "XXXX" ]; do echo "$line"; done
hello
goodbye
####
testing

This User Gave Thanks to RudiC For This Post:
# 3  
Old 07-22-2014
Code:
perl -ne 'last if /####/ or eof;print' infile

# 4  
Old 07-22-2014
You mysteriously switched "####" in your examples to "XXXX". Which is it?

Code:
sed -n "/####/q;p" file

# 5  
Old 07-22-2014
Quote:
Originally Posted by Scott
You mysteriously switched "####" in your examples to "XXXX". Which is it?

Code:
sed -n "/####/q;p" file

No mystery. My question is how to detect EOF when the defined delimiter is not present.

RudiC's answer was perfect. I want to do other things in the while loop with $line and I want to do things with the rest of stdin after the second delimiter line.

Mike
This User Gave Thanks to Michael Stora For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed command to replace a line at a specific line number with some other line

my requirement is, consider a file output cat output blah sdjfhjkd jsdfhjksdh sdfs 23423 sdfsdf sdf"sdfsdf"sdfsdf"""""dsf hellow there this doesnt look good et cetc etc etcetera i want to replace a line of line number 4 ("this doesnt look good") with some other line ... (3 Replies)
Discussion started by: vivek d r
3 Replies

2. Shell Programming and Scripting

To read specific line from a file

Hi, I have a ldif file like below: version: 1 dn: cn=Test Group,ou=Applications,dc=xyz,dc=com objectClass: groupOfUniqueNames objectClass: top cn: Test Group uniqueMember: uid=abc,ou=People,o=xyz,o=Corporate,dc=xyz,dc=com dn: cn=Test Sub Group,cn=Test... (4 Replies)
Discussion started by: saurau
4 Replies

3. Shell Programming and Scripting

How to read particular line in file from specific column?

Hi...friends.... I want to create inventory...information for that I need to read some specific row say 2nd row from 1st 3 column and and write data with particular file used, I have some more column also but I need only 3 column data of first entry after header I attached sample file..those... (12 Replies)
Discussion started by: nex_asp
12 Replies

4. Shell Programming and Scripting

Read file from nth line to specific character

Hi, I want to read the file from nth line (where n is an integer) to until I encounter @ char. Can any one please help me how to do this? Thanks. (3 Replies)
Discussion started by: laalesh
3 Replies

5. Shell Programming and Scripting

How to read from specific line number in shell script?

I have a text file which is having 30000 lines in it. I have to create a xml file for each 10000 lines until all the lines in the text files are written. Also please help how can i get number of lines in the text file in a shell variable? (19 Replies)
Discussion started by: vel4ever
19 Replies

6. Shell Programming and Scripting

Using awk to read a specific line and a specific field on that line.

Say the input was as follows: Brat 20 x 1000 32rf Pour 15 p 1621 05pr Dart 10 z 1111 22xx My program prompts for an input, what I want is to use the input to locate a specific field. Like if I type in, "Pou" then it would return "Pour" and just "Pour" I currently have this line but it is... (6 Replies)
Discussion started by: Bungkai
6 Replies

7. Shell Programming and Scripting

read a string from its end to its start and stop when you find a specific character

How can I do this? Actually I have a file which contains a path e.g. /home/john/Music/hello.mp3 and I want to take only the filename (hello.mp3) So, I need to read the file from its end to its start till the character "/" Is this possible? Thanks, I am sure you'll not disappoint me here! Oh,... (9 Replies)
Discussion started by: hakermania
9 Replies

8. Shell Programming and Scripting

How to read the value from a specific line and column BASH

Hi All, I have the same problem as the one posted in https://www.unix.com/shell-programming-scripting/96097-how-read-value-specific-line-column-csh-variable.html but I'm using bash. Can anyone tell me how I have to modify the code to make it bash compatible? eval `awk 'NR==3{print "set... (5 Replies)
Discussion started by: f_o_555
5 Replies

9. Programming

Stop exe from command line

Hi All, I want to find if a certain exe is running and if it is running i want to stop the execution of this exe (the exe has been created by me) on solaris. What are the unix command i need to use and can i put these comand in a shell script or create a new exe to do this task (i would... (4 Replies)
Discussion started by: zing
4 Replies

10. Shell Programming and Scripting

How to read the value from a specific line and column in to a csh variable

Hi All, Although its a basic question the last 2 hours of googling and trying didnt help me to achieve what i want. Maybe some one can help me I have a text file text1.txt: blablablabla A B C D E F and i would like to read to read what is on position E (line 3 column 2) in a... (2 Replies)
Discussion started by: Radamez
2 Replies
Login or Register to Ask a Question