How to Join Multiple Lines of File Names into One With Custom Delimiter

How to join multiple lines of file names into one with custom delimiter?

Similar to the very first option but omits the trailing delimiter

ls -1 | paste -sd "," -

How to merge multiple lines into single line but only for block of lines separated by blank line

Even a bit shorter than the version of John1024

awk 'BEGIN { RS=""; ORS="\n\n"}{$1=$1}1'

or

awk -v RS="" -v ORS="\n\n" '{$1=$1}1'

Using RS="" tells awk to use any paragraph as a record (i.e. a block of text separated by blank lines). But it also tells awk that a <newline> is always a field separator in combination with FS. By just redefining the output record separator ORS, we can output everything as you want by telling awk to redefine its record $0 by resetting the first record $1=$1. This has as effect that all field separators defined by FS (the default value here) and the newlines (due to RS="") are replaced by OFS (default a <space;>). Finally we print the record with 1

You can get rid of all the spaces when you additionally set OFS=""

RS The first character of the string value of RS shall be the input record separator; a <newline> by default. If RS contains more than one character, the results are unspecified. If RS is null, then records are separated by sequences consisting of a <newline> plus one or more blank lines, leading or trailing blank lines shall not result in empty records at the beginning or end of the input, and a <newline> shall always be a field separator, no matter what the value of FS is.

source: POSIX awk standard

Join multiple lines into One (.cap file) CentOS

You may use

cat file | awk  'BEGIN { FS = "\n"; RS = "\n\n"; ORS=";" } { gsub(/\n/, "", $0); print }' | sed 's/;;*$//' > output.file

Output:

Name:SidID:123;Name:JaiID:234;Name:ArunID:12

Notes:

  • FS = "\n" will set field separators to a newline`
  • RS = "\n\n" will set your record separators to double newline
  • gsub(/\n/, "", $0) will remove all newlines from a found record
  • sed 's/;;*$//' will remove the trailing ; added by awk

See the online demo

Bash to join a list

For a single-character delimiter, a fairly easy way would be to gather the items into an array, then expand the array with IFS set to the delimiter.

For example, for input in the form of lines read from the standard input, with the result written to the standard output, you could do this:

list_join() {
local IFS
local -a items
mapfile -t items
IFS=,
printf %s "${items[*]}"
}

Demo:

$ seq 3 | list_join
1,2,3
$

The same technique is applicable to input from other sources and output to other destinations, though the details (mapfile / printf) would need to be adjusted. The essentials are IFS and the array expansion ("${items[*]}").

How to correctly add new line to the file when multiple lines are added in parallel?

I simply put all files in one folder and stacked them without parallelisation

Concise and portable join on the Unix command-line

Perhaps a little surprisingly, paste is a good way to do this:

paste -s -d","

This won't deal with the empty lines you mentioned. For that, pipe your text through grep, first:

grep -v '^$' | paste -s -d"," -

Read file, multiple lines at a time (separated by delimiter)

Another approach with awk that should be portable across all awk varieties can use '=' as the field-separator, e.g.:

awk -F= '
$1~/[ ]*year/ { year = substr($2,2,match($2,/,/)-2) }
$1~/[ ]*month/ { month = substr($2,3,match($2,/,/)-4) }
$1~/[ ]*publisher/ { pub = substr($2,3,match($2,/,/)-4) }
FNR>1 && $1~/^@/ { print pub"\t"year"\t"month }
END { print pub"\t"year"\t"month }
' list.bib

Where each of the rules extracts either the year, month or publisher and trims the additional characters from either end of the wanted string using substr() and match(). The END rule is used to print the final set of values collected.

Example Use/Output

With your example data in list.bib, executing the command would result in:

awk -F= '
$1~/[ ]*year/ { year = substr($2,2,match($2,/,/)-2) }
$1~/[ ]*month/ { month = substr($2,3,match($2,/,/)-4) }
$1~/[ ]*publisher/ { pub = substr($2,3,match($2,/,/)-4) }
FNR>1 && $1~/^@/ { print pub"\t"year"\t"month }
END { print pub"\t"year"\t"month }
' list.bib
Wiley 2020 feb
Journal 2010 jul


Related Topics



Leave a reply



Submit