Hi friends
I have a file with two colums separated by Tab, the second colum had some numbers like the following:
koko 2
toto3 4
momo6 1
hoho 0
bobo5 3
I'm trying to make a script that give me the results for rows when the second colum value are greater than 1
I make the following as a bigen , can you help me please to complete it
#!/bin/bash
result=`cat file|cut -f 2`
for x in $result
do
if [ $x -gt 1 ]
then
grep $x
else
exit
fi
done
To keep the forums high quality for all users, please take the time to format your posts correctly.
First of all, use Code Tags when you post any code or data samples so others can easily read your code. You can easily do this by highlighting your code and then clicking on the # in the editing menu. (You can also type code tags
```text
and
```
by hand.)
Second, avoid adding color or different fonts and font size to your posts. Selective use of color to highlight a single word or phrase can be useful at times, but using color, in general, makes the forums harder to read, especially bright colors like red.
Third, be careful when you cut-and-paste, edit any odd characters and make sure all links are working property.
Thank You.
The UNIX and Linux Forums
---------- Post updated at 07:41 AM ---------- Previous update was at 07:40 AM ----------
trying a fix to your code I came up with the following:
#!/bin/sh
result=`cat file | tr '011' ' '| cut -f 2 -d' '`
for x in $result
do
if [ $x -gt 1 ]
then
grep " $x" file
fi
done
I got rid of the 'exit' as you won't process anything after a value that is less than or equal to 1 otherwise. I added the name of the file to the 'grep' command so it knew where to search for the match, and I added a 'tr' command to change the tab to a space and then cut get's a '-d' switch to separate the fields on that space.
This assumes you have only one tab separating the columns of data, and seems to work for me.
However, I would rewrite it like the following, so as to process this with one step inside a loop:
#!/bin/sh
while read c1 c2
do
if [ $c2 -gt 1 ]
then
printf "%s\t%s\n" $c1 $c2
fi
done < file
Using a while loop to loop through the file, the read command separates the output into two variables. Test the second variable, and if the criteria is met, just reconstruct the known format of the file with a printf statement, rather than greping the original file.
Caveat: I don't have access to 'bash' but I do have access to a bourne shell, so I used that. Probably shouldn't make a difference in this code.
No I didn't miss it. Yes it does solve the problem rather quickly and neatly.
What it doesn't do is improve the OP's shell scripting. Sure, he gets a working fix now, but the next time when he has to write a shell script to solve a different problem, and it turns out like his first attempt here, he's back for a quick fix again. "Give a man a fish, you feed him for a day ..."
I really missed it. I did. I thought it was an example to illustrate the forum editing standards and didn't read it.
But if you are learning, i think there is nothing wrong with see different solutions.