0

I have an input file of which a sample is:

8101010447 25.09 40n50.76  15e06.74   4.96  999  -----  0.2388 314  6.90 99.00  60   6 81EV00001
8101010929 23.85 42n47.96  12e47.90   8.90  3.0  MCSTI  0.0917 123  0.60  2.80  47  16 81EV00002
8101011100 14.41 40n52.23  15e20.57   7.34  999  -----  0.7021 123  0.40  1.30  67  11 81EV00003

and I need to get the last and fourth columns. I am using awk to get the columns correctly but the last column has a newline character at the end so the columns are outputted on separate lines.

My code is the following:

awk -F' ' '{print $14, $4}' catalog.txt >> bbb.txt

Any ideas on how I can do this?

The final output should look like:

81EV00001 15e06.74
81EV00002 12e47.99
81EV00003 15e20.57
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • 1
    Probably `awk '{print $NF, $4}' file` suffices, since `-F' '` is the default and `$NF` matches the last field. However, it is not clear how the output should look like. Please [edit] to clarify. – fedorqui Dec 08 '15 at 17:30
  • It should be 81EV00001 15e06.74 for the first line – user3540561 Dec 08 '15 at 17:32
  • Please provide the output you get, preferably with `cat -vET`. – Karoly Horvath Dec 08 '15 at 17:32
  • Your existing output has a spurious newline, or a spurious carriage return? (The latter seems far more likely). And if it's a CR... well, could you just strip them all? Do you want your output to have CRLFs, as your input does, or does unix-style LF-only work? – Charles Duffy Dec 08 '15 at 17:32
  • I don't understand the question. The last column of a file usually has a newline after it (unless you end each line with spaces). But `awk` doesn't include the newline in the `$14` value. – Barmar Dec 08 '15 at 17:43
  • Is my comment invisible? :) – Karoly Horvath Dec 08 '15 at 17:43
  • @KarolyHorvath not sure what you are asking – user3540561 Dec 08 '15 at 17:44
  • @Barmar it seems it is getting the newline as well – user3540561 Dec 08 '15 at 17:45
  • See [why-does-my-tool-output-overwrite-itself-and-how-do-i-fix-it](https://stackoverflow.com/questions/45772525/why-does-my-tool-output-overwrite-itself-and-how-do-i-fix-it). – Ed Morton Oct 07 '22 at 20:03

3 Answers3

4

You can remove the CR characters before printing:

awk '{ sub("\r", "", $14); print $14, $4; }' catalog.txt >> bbb.txt

The sub() function replaces one string with another string; in this case, I replace the \r character in column 14 with an empty string.

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • I've added an explanation, although it seems like it should be obvious, assuming you know what the `sub()` function does. – Barmar Dec 08 '15 at 17:50
2

I encountered this on a DOS file with \r\n at the end of the row. Therefore the final column had a \r tacked onto it because the default RS in awk/gawk is \n.

The solution is simple. Make the RS \r\n i.e.

RS="\r\n";
1

You might try these approaches, in order to get the fields (awk) and remove the unwanted symbols (tr):

Removing CR:

awk -F' ' '{print $14, $4}' catalog.txt | tr -d '\r' > bbb.txt

Removing LF:

awk -F' ' '{print $14, $4}' catalog.txt | tr -d '\n' > bbb.txt

Removing both CR and LF:

awk -F' ' '{print $14, $4}' catalog.txt | tr -d "\r\n" > bbb.txt
marcelovca90
  • 2,673
  • 3
  • 27
  • 34