0

UPDATED QS: I have been working on a bash script that will merge multiple text files with numerical values into one a single row text file using delimiter for each file values while merging

Example:

File1.txt has the followling contents:
168321099
File2.txt has:
151304
151555
File3.txt has:
16980925
File4.txt has:
154292
149092

Now i want a output.txt file like below:
, 168321099 151304 151555 16980925 , 154292 149092

Basically each file delimited by space and in a single row. with comma as first and 6 field of the outputrow

tried:

cat * > out.txt but its not coming as expected
RCzone
  • 51
  • 1
  • 7
  • Execute a Vim macro. The sequence would be (without apostrophes): "ggVGJZZ" which means: gg - go to beginning of the file, V- select entire lines, G - go to end of file, J - concatenate lines, ZZ - save and quit. Or - easier - execute search replace with sed : https://stackoverflow.com/questions/1251999/how-can-i-replace-a-newline-n-using-sed – Krzysztof Kaszkowiak May 14 '18 at 22:11
  • Possible duplicate of [How can I replace a newline (\n) using sed?](https://stackoverflow.com/questions/1251999/how-can-i-replace-a-newline-n-using-sed) – Krzysztof Kaszkowiak May 14 '18 at 22:13

4 Answers4

1

I am not very sure If I understood your question correctly, but I interpreted it as following :

  • The set of files file1,...,filen contain a set of words which you want to have printed in one single line.
  • Each word is space separated
  • In addition to the string of words, you want the first character to be a , and between word 4 and 5 you want to have a ,.

The cat+tr+awk solution:

$ cat <file1> ... <filen> | tr '\n' ' ' | awk '{$1=", "$1; $4=$4" ,"; print}'

The awk solution:

$ awk 'NR==1||NR==4{printf s",";s=" "}{printf " "$1}' <file1> ... <filen>
kvantour
  • 25,269
  • 4
  • 47
  • 72
0

If tr is available on your system you can do the following cat * | tr "\n" " " > out.txt

tr "\n" " " translates all line breaks to spaces

0

If the number of lines per file is constant, then the easiest way is tr as @Littlefinix suggested, with a couple of anonymous files to supply the commas, and an echo at the end to add an explicit newline to the output line:

cat <(echo ",") File1.txt File2.txt File3.txt <(echo ",") File4.txt | tr "\n" " " > out.txt; echo >> out.txt

out.txt is exactly what you specified:

, 168321099 151304 151555 16980925 , 154292 149092

If the number of lines per input file might vary (e.g., File2.txt has 3 or 4 lines, etc.), then placing the commas always in the 1st and 6th field will be more involved, and you'd probably need a script and not a one-liner.

landru27
  • 1,654
  • 12
  • 20
0

Following single awk could help you on same.

awk 'FNR==1{count++;} {printf("%s%s",count==1||(count==(ARGC-1)&&FNR==1)?", ":" ",$0)} END{print ""}' *.txt

Adding a non-one liner form of solution too now.

awk '
FNR==1        {   count++                                                         }
              {   printf("%s%s",count==1||(count==(ARGC-1)&&FNR==1)?", ":" ",$0)  }
END           {   print ""                                                        }
' *.txt
RavinderSingh13
  • 130,504
  • 14
  • 57
  • 93