0

Let's say, I have a file abc.txt and there is a pattern some_pattern in the file and I want to print first column of that line.

cat abc.txt | grep some_pattern | awk '{print $1}'

I know in advance that the pattern is present in first 10 lines of the file. So, wanted to optimize using:

head -10 abc.txt | grep some_pattern | awk '{print $1}'

But what if now I had to search for another pattern some_pattern2 which is again in first 10 lines of the file? I wish I could store the output of head command in some variable like:

trimmed_file=`head -10 abc.txt`
echo $trimmed_file | grep some_pattern | awk '{print $1}'

But unfortunately, $trimmed_file has first 10 lines of file with no newline character due to which there is just a single line and it always print the first word of file.

Vaibhav Nigam
  • 1,334
  • 12
  • 21

0 Answers0