0

I have two separate lines being consolidated onto one line. The issue is that at the current moment, it is being separated by a space. I would like to know how I can separate the two lines into one line with a Tab spacing instead.

Here is the code I currently have:

awk '/^loginName:/{x=NR+1}(NR<=x){print}'\t FinalResults.txt| sed "N;s/\n/ /g"

I see the tab command is %\t, but where exactly would this go? Also, can you break down the current coding and explain in detail what everything does and what is the purpose as I am trying to learn how bash works.

Additional information: The question is related to this answer: https://unix.stackexchange.com/a/502156/330217. The input file FinalResults.txt is the same as shown inthe other question.

  • Is this related to unix.stackexchange.com/q/502115/330217 ? Please show your input file FinalResults.txt and expected and actual output in the question. – Bodo Feb 22 at 15:20
  • It is, I gave you the best answer to that one. But this one was a shorter script so I was wondering how it worked and just focusing on the Tab command for this. I tried adding the %\t everywhere and no luck. I want it to show Name Tab Memory then next line. – Ibrahim A Feb 22 at 15:29
1

The awk script prints all lines starting with loginName: and the next line immediately following it regardless of its contents.

/^loginName:/{x=NR+1} # for matching lines store the record (line) number +1 into x
(NR<=x){print} # The condition will be true for the line matching the pattern above and the following line.

Read the documentation of sed and awk. See https://stackoverflow.com/q/6255796/10622916 for sed's N command.

Replace the sed command with

 sed "N;s/\n/\t/g"

to separate the lines with Tab instead of space.

Or change the awk script

awk '/^loginName:/{x=NR+1;l=$0}NR==x{print l "\t" $0}' FinalResults.txt

and omit the sed command.
If you want it even shorter

awk 'l{print l"\t"$0;l=0}/^loginName:/{l=$0}' FinalResults.txt

but a shorter script may be more difficult to understand.
(Here the condition for printing is the value of l being non-empty and not 0, and l stores the loginName: line.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.