I have a text log file
$ cat aaa
673 20160405 root "/path_to/gis/20160401/20160301_placement_map_org.dbf" ""
673 20160405 root "/path_to/gis/20160401/20160310_20160401ent_map_org.dbf" ""
790890 20170201 jle "/path_to/gis/20160401/Pina (Asc) 20160401 Rapid Report.kmz" ""
5883710 20160406 dho "/path_to/gis/20160401/20160401_Pina_Asc_Rapid_Report_Minesouth.pdf" ""
673 20160405 dho "/path_to/gis/20160401/20160310_20160401 placement map org.dbf" ""
Now I have this script output just the full path of the files:
#!/bin/bash
function nodatechk() {
arr=("$@")
for ((i=3;i<${#arr[@]};i+=5));
do
echo "${i}" "${arr[i]}"
done
}
r=( $(grep gis aaa) )
nodatechk "${r[@]}"
The output is break because of the 3rd line (and 5th line) have a space in the element, although it has double quote.
How can I fix this? (BTW I know I can use awk or cut to print out columns, but in this case I just want use grep.) Thanks.
man bash
.$(grep gis aaa)
undergoes word splitting, but literal double quotes aren't there, they're a result of the command substitution, so they don't influence the word splitting.awk '/gis/ && n++ % 5 == 3 {print n-1, $0}' < aaa
is simpler and would be several orders of magnitude faster for large inputs. You generally don't want to use shell loops to process text.grep
to output any columns. It is unclear what you want to be doing withgrep
to output the column that you are interested in.awk
would be a better choice for that. It's also unclear whether the columns are tab or space separated.gis
(anywhere in the line), treating a string enclosed in quotes as a single word, even if it contains space(s). It would be nice if you (1) said so, (2) showed what output you want, and (3) included some lines in your file that don't containgis
; as it is, the whole idea of doing this withgrep
seems misguided. … (Cont’d)""
, or might it be"foo"
— or might it be"foo bar"
(with embedded spaces)? … (Cont’d)