I have the following file:
test
1
My
2
Hi
3
i need a way to use cat ,grep or awk to give the following output:
test1
My2
Hi3
How can i achieve this in a single command? something like
cat file.txt | grep ... | awk ...
Note that its always a string followed by a number in the original text file.
Here is the answer:
cat file.txt | awk 'BEGIN { lno = 0 } { val=$0; if (lno % 2 == 1) {printf "%s\n", $0} else {printf "%s", $0}; ++lno}'
This takes consecutive lines and pastes them together delimited by the empty string.
sed 'N;s/\n//' file.txt
This should give the desired output when the content is in file.txt
EDIT: I just noticed that your question requires the use of
cat
andgrep
. Both of those programs are unnecessary to achieve your stated aims. If you have some reason for including them that you haven't mentioned, try this (uselessly inefficient) version of the line I wrote immediately above:It is possible that this command uses features not present in the original
awk
program. You may need to invoke the new awk program,nawk
instead.If your input file is always 1 number then 1 string, and you only want the strings, all you have to do is take every other line.
If you only want the odd lines, you can do
awk 'NR % 2' file.txt
If you want the evens, this becomes
awk 'NR % 2==0' data