catdotgif
catdotgif

Reputation: 1775

How can I prevent awk from appending newlines to a match

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

Answers (3)

bash-o-logist
bash-o-logist

Reputation: 6911

before you pass the file to awk, do a dos2unix on your file

Upvotes: 1

Michał Wojciechowski
Michał Wojciechowski

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

glglgl
glglgl

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

Related Questions