That's what awk
is for:
awk '{for(i=100;i<=NF;i+=100){printf "%s ",$i;} print ""}' file > output
Or, if you can have spaces inside your fields, specify tab as the field separator:
awk -F'\t' '{for(i=100;i<=NF;i+=100){printf "%s ",$i;} print ""}' file > output
Alternatively, you could use Perl:
perl -ane 'for($i=99;$i<=$#F;$i+=100){print "$F[$i] "}' file > output
To do this for multiple files, you can use a shell loop (assuming you want to run this on all files in the current directory):
for f in *; do
awk '{for(i=100;i<=NF;i+=100){printf "%s ",$i;} print ""}' "$f" > "$f".new;
done
Try this
$ awk -F, 'NR<2{split(gensub(/Citty/,"City","g",$0),a,FS)}NR==2{for(b=2;b<=NF;b+=2){c=c a[b]" "$b","}print gensub(/,$/,"",1,c)}NR>2{print gensub(/(^,|" *",)/,"","g",$0)}' inp
Product Name,City Location,Price Per Unit
banana,CA,5.7
apple,FL,2.3
$
Same code is more readable if split across a few lines :
$ awk -F, '
> NR<2{split(gensub(/Citty/,"City","g",$0),a,FS)}
> NR==2{for(b=2;b<=NF;b+=2){c=c a[b]" "$b","}print gensub(/,$/,"",1,c)}
> NR>2{print gensub(/(^,|" *",)/,"","g",$0)}' inp
Product Name,City Location,Price Per Unit
banana,CA,5.7
apple,FL,2.3
$
If 1st line, split the line into array elements within a. Fix the Citty->City typo.
If 2nd line, starting with the 2nd column, print the corresponding column from 1st line together with this column. Repeat for each column, going in 2 column increments. Strip the trailing ,
.
After 2nd line, replace any leading ,
or any "<spaces>",
with an empty string and then print the result.
Tested ok on GNU Awk 4.0.2
Try it online!
Best Answer
Contents of
test1.txt
Contents of
test2.txt
Sample.
Explanation
We're using the
-d
flag to set the delimiter to a ,