0

I have output with many rows. The number of rows on the output is inconsistent. Is there a way that the last row can be selected so that a character may be found and replaced? If not using awk or sed, any Unix* tool.

This is a simplified example of the input I need to modify. Typically it would be one to two hundred rows in length and varies. I would like to find the final comma and remove it.

Example Input

202", # 1233eda 
049", # 1022ats 
194", # 109nunnz 
180", # 0010204NE 
113", # 10195y.it 
489", # 1-420 
345", # 192EelL 
209", # 18tWa 
145", # 18tr 
137", # 1165y_SVM 
561", # 1odeN 
737", # 1hoer_co 
816", # 10138wDUSW 
954", # 1013saLN349ND 
969", # 112t-2_420 
114", # 1012i.fm 
650", # 10zy.i 
860", # 1119b.co 
177", # 1.0109tz

Desired Output

202", # 1233eda 
049", # 1022ats 
194", # 109nunnz 
180", # 0010204NE 
113", # 10195y.it 
489", # 1-420 
345", # 192EelL 
209", # 18tWa 
145", # 18tr 
137", # 1165y_SVM 
561", # 1odeN 
737", # 1hoer_co 
816", # 10138wDUSW 
954", # 1013saLN349ND 
969", # 112t-2_420 
114", # 1012i.fm 
650", # 10zy.i 
860", # 1119b.co 
177"  # 1.0109tz
2
  • Define nix tool, is it Unix or Nixos? Be precise, thanks Apr 16, 2023 at 21:16
  • I just mean nix as in unix. Sorry, not very clear about the definition. I'm on macOS.
    – XJMZX
    Apr 16, 2023 at 21:16

2 Answers 2

4

With sed:

$ sed '$s/,//' file
Operator Meaning
$ last line of file
s/TO_REPLACE/REMPLACEMENT/ substitution skeleton
, literal ','

With a Perl's one-liner:

$ perl -F, -ne 'if (eof()) {print join " ", @F} else {print}' file 

Output

202", # 1233eda 
049", # 1022ats 
194", # 109nunnz 
180", # 0010204NE 
113", # 10195y.it 
489", # 1-420 
345", # 192EelL 
209", # 18tWa 
145", # 18tr 
137", # 1165y_SVM 
561", # 1odeN 
737", # 1hoer_co 
816", # 10138wDUSW 
954", # 1013saLN349ND 
969", # 112t-2_420 
114", # 1012i.fm 
650", # 10zy.i 
860", # 1119b.co 
177"  # 1.0109tz
3
  • Thanks this answer gave me the desired output. If I could just ask one querstion: In the sed solution, why does the substitution skeleton need two backslashes after the literal?
    – XJMZX
    Apr 16, 2023 at 21:36
  • Because // mean 'nothing'. s/to replace/replaced/ Apr 16, 2023 at 21:41
  • Thanks, I've got the syntax now. Grateful for your help.
    – XJMZX
    Apr 16, 2023 at 21:42
0

If this is all you have to do with that input then I'd use the sed solution that @Gilles posted but since you already have sed and perl scripts and in case you actually need to do more than that, here's one way to do it in awk by just buffering 1 line:

$ awk 'NR>1{print p} {p=$0} END{sub(/,/," ",p); print p}' file
202", # 1233eda
049", # 1022ats
194", # 109nunnz
180", # 0010204NE
113", # 10195y.it
489", # 1-420
345", # 192EelL
209", # 18tWa
145", # 18tr
137", # 1165y_SVM
561", # 1odeN
737", # 1hoer_co
816", # 10138wDUSW
954", # 1013saLN349ND
969", # 112t-2_420
114", # 1012i.fm
650", # 10zy.i
860", # 1119b.co
177"  # 1.0109tz

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .