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. – choroba Dec 04 '18 at 23:34awk '/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. – Stéphane Chazelas Dec 05 '18 at 08:34grep
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. – Kusalananda Dec 06 '18 at 17:13gis
(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) – G-Man Says 'Reinstate Monica' Dec 07 '18 at 00:00""
, or might it be"foo"
— or might it be"foo bar"
(with embedded spaces)? … (Cont’d) – G-Man Says 'Reinstate Monica' Dec 07 '18 at 00:00"
, even if they're non-null and don't contain any blanks? And can the pathname possibly *contain any quote characters? … … … … … … … … … … … P.S. How would you do this withawk
and/orcut
, given that the column you want contains spaces?* – G-Man Says 'Reinstate Monica' Dec 07 '18 at 00:00awk '/gis/ { print $4 }'
, but without treating spaces between quotes as field separators. The whole every-fifth-thing logic is there only because they expect$(grep gis aaa)
to give them an array of words, with five words per line. – G-Man Says 'Reinstate Monica' Dec 07 '18 at 00:03