Register
It is currently Sat Sep 20, 2014 2:00 pm

how to select lines?


All times are UTC - 6 hours


Post new topic Reply to topic  [ 8 posts ] 
Author Message
 PostPosted: Tue Jul 21, 2009 7:23 am   

Joined: Tue Jul 21, 2009 7:21 am
Posts: 2
Hello all,

I have a question regarding a bash script:
how can I read a file and put in a array all the lines that are after each blank lines.

ex:

aaa
111

bbb
222
333

ccc
555

put in the array only aaa bbb ccc


Top
 Profile  
 PostPosted: Tue Jul 21, 2009 10:21 am   

Joined: Mon Nov 17, 2008 7:25 am
Posts: 221
If the file has special properties that you're looking for then grep is a useful tool :)

otherwise I guess a bunch of if's and stuff is needed, but it's doable in a one liner ;)
Code:
print_it=0; for i in $(cat test.txt|sed -e "s/^\(.*\)$/\"\1\"/"); do i=$(echo $i|sed -e "s/^\"\(.*\)\"/\1/"); [ $print_it -eq 1 ] && echo $i && print_it=0; print=$(echo $i|grep "^$" > /dev/null; echo $?); [ $print -eq 0 ] && print_it=1; done


To clarify it i'll split it up like a script :)
Code:
#!/bin/bash
# Just setting this to avoid a -eq unary operator error.
print_it=0
# Loop throu the text file. Since there's nasty whitespaces in this file we encase all the lines with quotation.
# Change test.txt to the file name.
for i in $(cat test.txt|sed -e "s/^\(.*\)$/\"\1\"/"); do
    # And now we remove it for this particular itteration
    i=$(echo $i|sed -e "s/^\"\(.*\)\"/\1/")
    # Check if the previous spin yielded a "blank" row
    if [ $print_it -eq 1 ];then
        # If it did, print the line and reset the variable.
        echo $i
        print_it=0
    fi
    # Get status code for the content of the current row to see if it's a blank or not.
    print=$(echo $i|grep "^$" > /dev/null; echo $?)
    # If it was a blank we set the variable for the next itteration
    if [ $print -eq 0 ]; then
        print_it=1
    fi
done


sajko@hanna:~> sh test.sh
aaa
bbb
ccc

from a file looking like this:
Code:

aaa
111

bbb
222
333

ccc
555


If the file has a special formatting it's probably easier to check for occurrences, but since you only supplied that the file contains blank rows and then the actual thing you're looking for is this is the best I can do.

Hope this gives you a clue anyway :)

Best regards
Fredrik Eriksson


Top
 Profile  
 PostPosted: Tue Jul 21, 2009 1:44 pm   

Joined: Tue Jul 21, 2009 7:21 am
Posts: 2
Thanks a lot!


Top
 Profile  
 PostPosted: Wed Jun 22, 2011 4:39 pm   
User avatar

Joined: Wed Jun 08, 2011 8:27 am
Posts: 189
Location: outer Shpongolia
Whoa. awk(1) could do the job in one command. (not counting the print one)

Code:
array=("$(awk '!NF {getline; print}' file)")


But, still, in pure bash(1):

Code:
while read -r; do
    if [[ ! $REPLY ]]; then
        read -r
        array+=("$REPLY")
    fi 
done < file


Top
 Profile  
 PostPosted: Wed Jun 22, 2011 11:02 pm   
User avatar

Joined: Sun Jun 27, 2010 12:57 am
Posts: 192
I don't think the OP will read this anymore after 2 years...


Top
 Profile  
 PostPosted: Fri Jun 24, 2011 9:00 am   
User avatar

Joined: Wed Jun 08, 2011 8:27 am
Posts: 189
Location: outer Shpongolia
Sure, but I wanted to share a proper solution.

And here it is, without the use of the getline command:

Code:
array=("$(awk 'c && !--c; !NF {c = 1}' file)")


Top
 Profile  
 PostPosted: Fri Jun 24, 2011 11:51 am   
User avatar

Joined: Tue Apr 27, 2010 2:28 pm
Posts: 172
Location: Czech Republic
You can also use this, if your file does not contain -- :
Code:
grep -A1 ^$ | grep -v '^$\|--'


Top
 Profile  
 PostPosted: Mon Jun 27, 2011 6:28 pm   
User avatar

Joined: Wed Jun 08, 2011 8:27 am
Posts: 189
Location: outer Shpongolia
choroba wrote:
You can also use this, if your file does not contain -- :
Code:
grep -A1 ^$ | grep -v '^$\|--'


Ah yes, nice approach, but unfortunely grep(1) adds -- with -A, -B, -C options...

It made me think of the sed(1) solution:

Code:
array=("$(sed -n '/^$/ {n;p;}')")


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 8 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 4 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP