0

I have a lot of simple txt files. Each of them has only one string, e.g.

data_1

data_2

data_3

data_4

Each of files have unique name, e.g.

Name_01_apr_29

Name_02_apr_29

Name_03_apr_29

How to combine file content and file name?

I need:

data_1;Name_01_apr_29

data_2;Name_02_apr_29

data_3;Name_03_apr_29

etc.

I need to do that in some of linux command or in bash (some script.sh) because I need to start this request periodically in cron.

I'm working on debin 8.

Bart
  • 1
  • You can use `awk '{print $0";"FILENAME ;exit}' Name_01_apr_29` to combine the filename and its content. You may need to adjust the output as per requirement. – P.... Apr 29 '19 at 18:03

1 Answers1

0

An awk solution is simple:

awk '{print $0 ";" FILENAME}' Name_*_apr_29

An other method is using grep. You need to rearrange the output:

grep . Name_*_apr_29 | cut -d: -f2,1 | tr ':'  ';'

or

grep . Name_*_apr_29 | sed -r 's/([^:]*):(.*)/\2;\1/'
Walter A
  • 19,067
  • 2
  • 23
  • 43