Reputation: 251
I'm very inexperienced with shell scripts, and I need to write one that deletes an entire row when a column named Views contains the value 0. The column "Views" may not always be in the same location in the file, so I would need some way to find the location of the column before hand. Is this something that is feasible with sed or awk? Or is there something else that I can use?
Thanks!
Upvotes: 1
Views: 3160
Reputation: 1
awk '($1 == "badString") && !($1 ~ /[.]/) { next } 1' inputfile > outputfile
#if first column = badString or has . (dot) dont include it in outputfile
Upvotes: 0
Reputation: 8402
awk -F ',' 'NR==1{print;for(i=1;i<=NF;++i){if($i=="Views"){x=$i;y=i}}};NR>1{if($y!=0){print}}' file > new_file
breakdown of code
NR==1{ #for the first line
print #print it
for(i=1;i<=NF;++i){ #make a loop to read all the column and find the
if($i=="Views"){ #name "Views" in the first row.
y=i #Save the column number in a variable named y
}
}
}
NR>1{ # start from line 2 going downwards targeting
if($y!=0){ # the Views Column
print #if it does not contain 0, print the line
}
}
Upvotes: 0
Reputation: 44063
With awk, this could be done like this:
awk -F, 'NR == 1 { for(i = 1; i <= NF; ++i) { col[$i] = i }; next } $col["Views"] != 0' filename.csv
-F,
sets the field separator to a comma, since you mentioned a CSV file.
The code is
NR == 1 { # in the first line
for(i = 1; i <= NF; ++i) { # go through all fields
col[$i] = i # remember their index by name.
# ($i is the ith field)
}
next # and do nothing else
}
$col["Views"] != 0 # after that, select lines in which the field in
# the column that was titled "Views" is not zero,
# and do the default action on them (i.e., print)
Note that this will only filter out lines where the Views
column is exactly 0. If you also want to filter out lines where the Views
field is empty, use $col["Views"]
instead of $col["Views"] != 0
.
Upvotes: 4