Reputation: 1775
I'm writing a bash script to parse some fields from a tab delimited text file and append them to the filename of the file itself. I can parse the fields out just fine using awk, but they come with a newline appended. I would like to either strip out those newlines from the result or ideally prevent awk from appending them in the first place. Sample code with just an echo out of the stored string for now:
#!/bin/bash
echo "Usage: sh extract.sh filename.txt (or *.txt)"
for filenam in "$@"
do
timestring=$(awk 'BEGIN{ FS="\t"; RS="\n"; ORS="_"; OFS="_"}
/Conditions/ {printf $2}
/Date/ {printf $2}
/Time/ {printf $2}
END {}' $filenam)
echo $timestring
done
At the moment, every time it finds a match, it overwrites any previous matches, because of the \n appended to the end of the string. How do I prevent that? (so that I have conditions_date_time as a string, without any newlines).
Sorry if this seems like a simple question, but I've been googling for hours and tried all manner of things and I'm stumped. Thanks!
Upvotes: 3
Views: 240
Reputation: 2490
In your script, use print
instead of printf
, e.g. print $2
.
As for the overwriting, this could happen if the text file that you're processing has \r\n
line endings, instead of \n
. If this is the case, set the input record separator to \r\n
, e.g. RS="\r\n"
.
Upvotes: 1
Reputation: 91017
Try something like
#!/bin/bash
echo "Usage: sh extract.sh filename.txt (or *.txt)"
for filenam in "$@"
do
timestring=$(awk 'BEGIN{ FS="\t"; RS="\n"; ORS="_"; OFS="_"}
/Conditions/ { cond=$2 }
/Date/ { date=$2 }
/Time/ { time=$2 }
{ if (length(cond) && length(date) && length(time)) {
print cond "_" date "_" time;
cond = ""; date=""; time=""
}
}
END {}' $filenam)
echo $timestring
done
It's untested, but you should get the idea.
Upvotes: 0