Reputation: 695
I have a text file that looks like this:
This is the first line 80 55 75%
Second line 25 95 74%
The next line 50 50 50%
As you can see, on the left side the number of words will vary. Currently, if I use a command like this:
awk 'NR == 1 {print $1,$2,$3,$4}' file
This works out great for the first line and will return:
This is the first line
But if I do this on the second line:
awk 'NR == 2 {print $1,$2,$3,$4}' file
I get this, as expected:
Second line 25 95
But this is not what I need. For scripting purposes, I need awk, or perhaps another tool to "see" anything up to the second column of numbers as a single variable. So regardless of the number of words on the left side, they will always be seen as a single variable. How can I achieve this?
NOTE: I may have lines like this:
This is a testmarket001 65 45 25%
So I don't think you can just tell awk to print all words up to the first number, as it would cut off part of the line.
Upvotes: 1
Views: 272
Reputation: 67557
If your fields are tab delimited, just set the delimiter for awk
$ paste file1 file2 file3 | awk -F'\t' '{print $1}'
should work.
Note that column
command modifies the tabs. If your file is saved after column
formatting, you can try this
$ awk '{for(i=1;i<NF-2;i++) printf "%s ",$i; print ""}' file
This is the first line
Second line
The next line
Upvotes: 1
Reputation: 786031
If there are always 3 whitespace separates words at the end of each line you can use this awk
to get your output:
awk '{gsub(/(\s+\S+){3}\s*$/, "")} 1' file
This is the first line
Second line
The next line
You may also use sed
:
sed -E 's/(\s+\S+){3}\s*$//' file
Upvotes: 1