adding character at the end of each string in a file

Question:

I have a txt file that contains a single column of single words as such:

windfall
winnable
winner
winners
winning

I want to use the words in the file as regex strings for a mapping jobs. When finished the words should look like this:

windfall|winnable|winner|winners|winning

I need to use python or awk to open the file, place a | at the end of each and write the new content to a new file with the new character added and the column converted to a single horizontal line.

any suggestions?

Asked By: DataGuy

||

Answers:

Simplest is tr:

tr 'n' '|' < file.txt
Answered By: John Zwinck

Using Python you could do:

with open('oldfile.txt') as fin:
    with open('newfile.txt', 'w') as fout:
        fout.write('|'.join(map(str.strip, fin)))

The str.split removes newlines and whitespaces, while the join concatenates the lines with |.

Answered By: MSeifert

Using sed:

$ cat file
windfall
winnable
winner
winners
winning
$ sed ':a;N;s/n/|/;ba' file
windfall|winnable|winner|winners|winning
  • Create a loop using :a
  • Load the new line N in to execution space
  • substitute the newline with pipe
  • rinse and repeat.
Answered By: jaypal singh

In awk, if you don’t want the trailing |:

$ awk '{ s=s (NR>1"?"|":"") $0 } END { print s }' file
windfall|winnable|winner|winners|winning

The original version with getline which was basically an (not even the) outcome of an awk jamming session was:

$ awk 'BEGIN {
           while(r=getline) {         # read until EOF
               s=s (p==r?"|":"") $0;  # pile it to s, preceed with | after the first
               p=r                    # p revious r eturn value of getline
           } print s                  # out with the pile
       }' file
windfall|winnable|winner|winners|winning
Answered By: James Brown
awk -v RS= -v OFS="|" '/ /{next}$1=$1' file

windfall|winnable|winner|winners|winning
Answered By: Claes Wikner

Use paste:

$ cat /tmp/so.txt
windfall
winnable
winner
winners
winning

$ paste -sd'|' /tmp/so.txt
windfall|winnable|winner|winners|winning
Answered By: dawg

assuming no blank lines in between rows, and input is smaller than 500 MB, then better to keep it simple :

echo 'windfall
      winnable
      winner
      winners
      winning' | 
{m,g,n}awk NF=NF RS= OFS='|'
windfall|winnable|winner|winners|winning
Answered By: RARE Kpop Manifesto
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.