Welcome to the most active Linux Forum on the web.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 03-15-2012, 01:29 PM   #1
LQ Newbie
Registered: Oct 2011
Location: Athens, Greece
Posts: 16

Rep: Reputation: Disabled
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
if ($cnr>3) then
printf "%s \n" $usr >> mc.txt
printf "%s \n" $usr >> lc.txt;
done < usrccnr.txt;

Could you please help?
Old 03-15-2012, 01:36 PM   #2
LQ Newbie
Registered: Mar 2012
Distribution: Redhat, Solaris, AIX, HPUX
Posts: 8

Rep: Reputation: Disabled
cat usrccr.txt | while read a b
if [ $a -gt 3 ]
echo "$a $b" >> mc.txt
echo "$a $b" >> lc.txt
Old 03-15-2012, 01:39 PM   #3
LQ Newbie
Registered: Oct 2011
Location: Athens, Greece
Posts: 16

Original Poster
Rep: Reputation: Disabled
I should have read more! Thnx. It's been too many hours. SOLVED
Old 03-15-2012, 02:03 PM   #4
David the H.
Bash Guru
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian + kde 4 / 5
Posts: 6,834

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
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.

awk '{ if ( $1 > 3 ) { print $2 > "mc.txt" } else { print $2 > "lc.txt" } }' usrccnr.txt
Here are a few useful awk references:

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.

while read cnr usr ; do

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

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.

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


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
basic html question - download link to files on my webpage question Davno Linux - Server 5 12-25-2009 08:24 AM
linux distro question & mysql install question natalie.aloi Linux - Newbie 5 07-19-2009 09:28 PM
Question, Apples Contribution to Open Source + MacOs file structure question Higgy3k Other *NIX 5 07-25-2005 05:23 AM
Not your regular GRUB question - just a short question for a fried MBR!! ziphem Linux - General 3 01-31-2005 02:51 PM
login prompt question & kde scheme question JustinCoyan Slackware 2 06-09-2004 03:02 PM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 11:09 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration