10

I have these diff results saved to a file:

bash-3.00$ cat /tmp/voo
18633a18634
> sashabrokerSTP
18634a18636
> sashatraderSTP
21545a21548
> yheemustr

I just really need the logins:

bash-3.00$ cat /tmp/voo | egrep ">|<"
> sashaSTP
> sasha
> yhee
bash-3.00$

But when I try to iterate through them and just print the names I get errors. I just do not understand the fundamentals of using "if" with "while loops". Ultimately, I want to use the while loop because I want to do something to the lines - and apparently while only loads one line into memory at a time, as opposed to the whole file at once.

bash-3.00$ while read line; do  if [[ $line =~ "<" ]] ; then  echo $line ; fi ;  done <  /tmp/voo
bash-3.00$
bash-3.00$
bash-3.00$ while read line; do  if [[ egrep "<" $line ]] ; then  echo $line ; fi ;  done    <  /tmp/voo
bash: conditional binary operator expected
bash: syntax error near `"<"'
bash-3.00$
bash-3.00$ while read line; do  if [[ egrep ">|<" $line ]] ; then  echo $line ; fi ;  done <  /tmp/voo
bash: conditional binary operator expected
bash: syntax error near `|<"'
bash-3.00$

There has to be a way to loop through the file and then do something to each line. Like this:

bash-3.00$ while read line; do  if [[ $line =~ ">" ]];
 then echo $line |  tr ">" "+" ;
 if [[ $line =~ "<" ]];
 then echo $line | tr "<" "-" ;
 fi ;
 fi ;
 done  < /tmp/voo


+ sashab
+ sashat
+ yhee
bash-3.00$
1
  • Don't quote your regex in Bash 4.x. Commented Dec 3, 2013 at 20:27

3 Answers 3

10

You should be checking for >, not <, no?

while read line; do
    if [[ $line =~ ">" ]]; then
        echo $line
    fi
done < /tmp/voo
Sign up to request clarification or add additional context in comments.

1 Comment

sometimes on the diff the angle brackets go either way. Usually signifying > and add and < as a delte. Ultimately I want to sed-subistitue the '<' for "has been added: $line " and '>' "has been deleted: $line"
6

Do you really need regex here? The following shell glob can also work:

while read line; do [[ "$line" == ">"* ]] && echo "$line"; done < /tmp/voo

OR use AWK:

awk '/^>/ { print "processing: " $0 }' /tmp/voo

Comments

1

grep will do:

$ grep -oP '> \K\w+' <<END
18633a18634
> sashabrokerSTP
18634a18636
> sashatraderSTP
21545a21548
> yheemustr
END
sashabrokerSTP
sashatraderSTP
yheemustr

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.