ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
can you please provide explanation of this command "paste -sd" is working?
When used with the -s option, paste takes a single file and changes the content into one long line.
The individual lines are separated by a tab character by default. If you prefer a different delimiter character, the -d option is used to specify the desired delimiter. Even better, the -d option accepts a character string and uses each of the delimiter string characters in sequence. Coding -d"__\n" directs paste to use a blank, then a blank, then a NewLine character, then another blank, another blank, another NewLine, etc.
The two options -s and -d"__\n" may be combined as -sd"__ \n".
You may better understand this behavior of paste by experimenting with visible delimiter strings. For example, try the same paste with -sd"ABC" and you will see how those delimiter characters are "plugged in" as needed.
Your sed solution reveals an ambiguity in OP's problem statement. I interpreted it to have an input file which does not have a blank last line. In that case your sed fails.
Changing the InFile such that it does have a blank last line, your sed works and my awk fails. However, trusting that the last line is blank permits a shorter, cleaner awk solution...
Code:
awk '{ol=ol$0" "; if (NF==0) {print ol; ol=""}}' $InFile >$OutFile
/tmp$ cat in
Mark
Male 26 California
Lasi
Female 25 Florida
Andrew
Male 20 Boston
/tmp$ printf "%s\n" 'g/./s/$/ /\' j ,p | ed in
71
Mark Male 26 California
Lasi Female 25 Florida
Andrew Male 20 Boston
?
Number 71 and question mark go to stderr and can be ignored.
This command works as follows.
printf is used to append newline after each argument, for instance:
Code:
$ printf "%s\n" a b c
a
b
c
Actual arguments are commands for ed text editor.
g/./ is the so called global command applied to each non-empty line.
Here is the description from info ed commands:
Quote:
`(1,$)g /RE/COMMAND-LIST'
Global command. Applies COMMAND-LIST to each of the addressed
lines matching a regular expression RE. The current address is set
to the line currently matched before COMMAND-LIST is executed. At
the end of the `g' command, the current address is set to the last
line affected by COMMAND-LIST.
At least the first command of COMMAND-LIST must appear on the same
line as the `g' command. All lines of a multi-line COMMAND-LIST
except the last line must be terminated with a backslash (`\').
Any commands are allowed, except for `g', `G', `v', and `V'. By
default, a newline alone in COMMAND-LIST is equivalent to a `p'
command. If `ed' is invoked with the command-line option `-G',
then a newline in COMMAND-LIST is equivalent to a `.+1p' command.
Our COMMAND-LIST consists of two commands: 1) s/$/ / -- append one space to end of matched line
and 2) j -- join current line with the following one.
Final command ,p prints (p) whole buffer (,) to stdout. We may replace it by the w (write)
command to save changes to file.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.