+ 2
I wrote a script grep -w $1 -r $2|cut -d , -f 1,3|cut -d : -f 2 > $1.txt and want to understand what does cut -d : -f 2 do?
Script is [grep -w $1 -r $2|cut -d , -f 1,3|cut -d : -f 2 > $1.txt] I know what the grep part and what cut -d , -f 1,3 does. I don't understand the : in the second cut argument though? The file for example consists of a date,species,number. 2012-11-05,deer,5 2012-11-05,rabbit,22 2012-11-05,raccoon,7 2012-11-06,rabbit,19 2012-11-06,deer,2 2012-11-06,fox,4 2012-11-07,rabbit,16 2012-11-07,bear,1 Run script Bash Script.sh rabbit . Grep gets all the rabbits Cut the date and number field What does the last cut do?
4 Answers
+ 2
Hi Eric đ
cut is like his name it cuts or print the selected part of an input (file or piped data..) to the std output, and the '-d' flag is the delimiter and -f is the fields:
like if the input is something like the following :
Username;email;date1;job;location
...
Username9;email9;date9;job9;location9
...
and you do execute the following command :
$ cut -d ';' -f 1,5 input.txt
the output will look like this:
Username;location
...
Username9;location9
like you told your bash [the delimiter is ';' , give me the first and fifth fields from the input file]
you can add '-s' flag to do not print lines not containing delimiters
+ 1
the character or string after the '-d' flag is the delimiter, first here the delimiter is ',' and then ':' , okay I'll explain this line:
first its grep/select only those lines containing matches that form '$1' word which is the first argument gived to your script, -r means recursively without following symbolic links. and the result will be piped to the next command and as I said before, its select these specific fields according to that specific delimiter etc.. and last write it down to the file $1.txt ..
+ 1
Amine Laaboudi yo my bad I was having a brain fart. I was also just to focused on that one file but it recursively searches the directory as you said and it looks through all files.
0
Hi Amine Laaboudi thanks for the explanation I get that. Sorry I forgot to add the script in my question but it's in my description. Here it is though
grep -w $1 -r $2|cut -d , -f 1,3|cut -d : -f 2 > $1.txt
I also place an example file. So in the file there is no : so what does cut -d : -f 2 do in my script above?