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 ofRS
shall be the input record separator; a <newline> by default. IfRS
contains more than one character, the results are unspecified. IfRS
isnull
, 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 ofFS
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 newlinegsub(/\n/, "", $0)
will remove all newlines from a found recordsed 's/;;*$//'
will remove the trailing;
added byawk
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
How to Find Which Position a Word Is in a String
How to Exclude a Directory When Using 'Find'
How to Change Permissions For a Folder and Its Subfolders/Files
What Is Rss and Vsz in Linux Memory Management
The 'Eval' Command in Bash and Its Typical Uses
Forcing Bash to Expand Variables in a String Loaded from a File
What's the Difference Between "Statically Linked" and "Not a Dynamic Executable" from Linux Ldd
How to Run Multiple Background Commands in Bash in a Single Line
How to Escape Spaces in Path For Scp Copy in Linux
Remove a Specific Character Using Awk or Sed
How to Store a Command in a Variable in a Shell Script
Redirect All Output to File in Bash
Understanding Linux /Proc/Pid/Maps or /Proc/Self/Maps
Static Link of Shared Library Function in Gcc
Asynchronous Io Io_Submit Latency in Ubuntu Linux