4

I would like to convert my one colum of many rows into one line:

example :

1
1
1
1
1
2
2
2
3
3
4

and I want :

11111222334

any suggestion please?

1

3 Answers 3

6

paste:

$ paste -s -d "" file
11111222334

squeeze all lines together with no delimiter.

or these solutions that delete even the trailing newline

perl -pe chomp file
awk -v ORS="" 1 file
4

You could delete all newline characters with tr:

tr -d \\n <infile

Note there's no trailing newline character in the output so, if that's needed (e.g. to redirect to another file):

{ tr -d \\n <infile; printf \\n; } >outfile

If, instead, you need the fields separated by a space you could run:

row=$(tr \\n \   <infile)
printf %s\\n "${row%?}" >outfile

though paste really shines here...

2

You can also try echoing the command and sendig the output to a new file.

$ echo $(cat file) >> rows.txt

To prevent whitespaces between each line you can use sed in the same line

$ echo $(cat file) | sed 's/ //g' >> rows.txt
2
  • 1
    This will have strange results if the file contains characters in \[*?. It'll also strip off all whitespace, which may or may not (probably not) have been desired. Sep 30, 2015 at 22:54
  • @Gilles for the example given, I think this will work while the OP is not specifying the characters you mentioned, what do you think?
    – tachomi
    Sep 30, 2015 at 22:57

You must log in to answer this question.

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