0

I have a 10000 line file that contains on each line a string in the form of "data:key", which is also right-padded by 8 characters, where ':' is the delimiter. I am attempting to use awk from within Linux to print these pairs on their own lines, so that line #1 = data and line #2 = key, and I have achieved this using the command:

awk -F: '{print $1; print$2}' < ~/prices.txt

My problem occurs on the second line of each set. For some reason, it is padded with as much whitespace as there was from removing the data from the line. So, if my line was "26900:9976", the first line would be '26900' and the second line would be ' 9976', whitespace included.

If curious, I want to do it this way because I am piping the results to db_load to use within a B+-tree.

4

2 回答 2

0

Not exactly your answer but you can use tr for this:

 tr ':' '\n' < input

also I don't see the behaviour you are describing with your awk command, however, you can always add a sed to the pipeline to remove leading white space:

 tr ':' '\n' < ~/prices.txt | sed 's/^[ \t]*//'
 awk -F: '{print $1; print$2}' < ~/prices.txt | sed 's/^[ \t]*//'
于 2013-03-30T01:06:39.070 回答
0

You can use a regular expression as the field separator: a colon followed by zero or more whitespace chars will separate the fields.

awk -F ':[[:space:]]*' '{print $1; print $2}' < ~/prices.txt
于 2013-03-30T01:46:24.010 回答