Help with add existing file name as new data column in new output file

Input File 1

cat S1.txt
MI0043 2731 miR-1

Input File 2

cat S4.txt
MI006 310 CiR-1
MI057 10 CiR-24
MI750 5 CiR-24

Desired Output File 1

cat S1.txt.out
MI0043 2731 miR-1 S1.txt

Desired Output File 2

cat S4.txt.out
MI006 310 CiR-1 S4.txt
MI057 10 CiR-24 S4.txt
MI750 5 CiR-24 S4.txt

Do anybody have idea how to add the existing file name at the last data column in new output file?

Try

awk '{print $0, FILENAME}' S?.txt

Make very sure your input files don't have DOS <CR> line separators (they do have now!) cause that would overwrite the BOL with the file name.

1 Like

Hello perl_beginer,

Could you please try following and let us know if this helps.

awk 'FNR==1{I++} {print $0 OFS FILENAME > "OUTPUT_"I;}' S1.txt  S4.txt

EDIT: Adding one more solution with the requested output file names as follows.

awk '{print $0 OFS FILENAME >> FILENAME".out"}' S1.txt  S4.txt

This above will create 2 files named S4.txt.out and S1.txt.out .

Thanks,
R. Singh

1 Like

Thanks RudiC.
It worked fine :slight_smile:

---------- Post updated at 09:07 PM ---------- Previous update was at 09:06 PM ----------

HI RavinderSingh13,

Really many thanks :slight_smile:
Both solution looks fine and really thanks for your help.