LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   While IF question (https://www.linuxquestions.org/questions/linux-newbie-8/while-if-question-934627/)

t.othoneos 03-15-2012 12:29 PM

While IF question
 
Hei everyone,

I have a text file usrccnr.txt like so:

53 fefwgergre
33 grgergrgeer
31 gregregrgeg
13 greregregreghkuy
13 kjkjkgkjg
12 mhlkkgfpdhj
12 mlglfjdkz
12 jgfjdahu
12 ngrkeow
12 nfrkeoqeirw
11 mfklewqjeo
11 mkfdlajfkrl
11 klrepkgowperw
11 coawerw
11 ferfjrqwh
10 erwqjfioreq
10 fkreqwjreq
8 gkrelwpgjkreo
7 jireojwfirojgre
6 korepwkpgre
4 kgropqejgorqe
4 gramk
4 rgek
3 ned
3 grogpekrwe
2 vlvrew
2 vasrer
2 spsi
2 resi
2 gregregw
2 frekroekgoe
2 grelkre
2 mafm
2 grearge
2 gregrga
2 lerkwofkrso
2 grod
2 mlkremwkrg
2 mrkemwegrihw
2 mkrejwgmrweogrew
2 grlewmgrewmg
2 rlewmgleg
2 antgrewgwe
2 anagirgerse
2 agrekor
1 trwegtmkr
1 gregrew
1 greewrge
1 zourgrew
1 zonkergeqzs
1 gremrkea

What I am trying to do is create 2 seperate files containing: all text with the number 4 and higher to go to one file and the rest to another one.

This is the code I came up with but NO, it does not work:

while read $cnr $usr
do
if ($cnr>3) then
printf "%s \n" $usr >> mc.txt
else
printf "%s \n" $usr >> lc.txt;
fi
done < usrccnr.txt;

Could you please help?

dougt 03-15-2012 12:36 PM

cat usrccr.txt | while read a b
do
if [ $a -gt 3 ]
then
echo "$a $b" >> mc.txt
else
echo "$a $b" >> lc.txt
fi
done

t.othoneos 03-15-2012 12:39 PM

I should have read more! Thnx. It's been too many hours. SOLVED

David the H. 03-15-2012 01:03 PM

Please use [code][/code] tags around your code and data, to preserve formatting and to improve readability. Please do not use quote tags, colors, or other fancy formatting.

Processing of field-based input can usually be handled more efficiently with awk.

Code:

awk '{ if ( $1 > 3 ) { print $2 > "mc.txt" } else { print $2 > "lc.txt" } }' usrccnr.txt
Here are a few useful awk references:
http://www.grymoire.com/Unix/Awk.html
http://www.gnu.org/software/gawk/man...ode/index.html
http://www.pement.org/awk/awk1line.txt
http://www.catonmat.net/blog/awk-one...ined-part-one/


As for your original code, you got the basic idea right. But there are some syntax errors (you don't use "$" when setting variables, for example), and you need to use a proper set of arithmetic evaluation brackets for the test.

Code:

while read cnr usr ; do

        if (( cnr > 3 )); then
                printf "%s\n" "$usr" >> mc.txt
        else
                printf "%s\n" "$usr" >> lc.txt
        fi

done < usrccnr.txt

And remember, QUOTE ALL OF YOUR VARIABLE SUBSTITUTIONS. You should never leave the quotes off a variable expansion unless you explicitly want the resulting string to be word-split by the shell. This is a vitally important concept in scripting, so train yourself to do it correctly now. You can learn about the exceptions later.

http://mywiki.wooledge.org/Arguments
http://mywiki.wooledge.org/WordSplitting
http://mywiki.wooledge.org/Quotes



I also recommend taking the time to more cleanly format your code, as I did above.


All times are GMT -5. The time now is 03:02 PM.