LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-16-2014, 07:46 PM   #1
jone kim
Member
 
Registered: Apr 2010
Posts: 185

Rep: Reputation: 16
move the row of data up or down


I've to shift the row, to put them in a orderd way.

Sample text:
Mark
Male 26 California

Lasi
Female 25 Florida

Andrew
Male 20 Boston

Desired o/p:

Mark Male 26 California
Lasi Female 25 Florida
Andrew Male 20 Boston

What sed/awk command can be used to get the result?
 
Old 09-16-2014, 08:19 PM   #2
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,670

Rep: Reputation: 542Reputation: 542Reputation: 542Reputation: 542Reputation: 542Reputation: 542
With this InFile...
Code:
Mark
Male 26 California

Lasi
Female 25 Florida

Andrew
Male 20 Boston
... this paste ...
Code:
paste -sd"  \n" $InFile >$OutFile
... produced this OutFile ...
Code:
Mark Male 26 California 
Lasi Female 25 Florida 
Andrew Male 20 Boston
Daniel B. Martin
 
Old 09-16-2014, 08:31 PM   #3
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,670

Rep: Reputation: 542Reputation: 542Reputation: 542Reputation: 542Reputation: 542Reputation: 542
With this InFile...
Code:
Mark
Male 26 California

Lasi
Female 25 Florida

Andrew
Male 20 Boston
... this awk ...
Code:
awk '{ol=ol$0" "; if (NF==0) {print ol; ol=""}} END{print ol}' $InFile >$OutFile
... produced this OutFile ...
Code:
Mark Male 26 California 
Lasi Female 25 Florida 
Andrew Male 20 Boston
Daniel B. Martin
 
Old 09-17-2014, 07:40 PM   #4
jone kim
Member
 
Registered: Apr 2010
Posts: 185

Original Poster
Rep: Reputation: 16
Quote:
Originally Posted by danielbmartin View Post
With this InFile...
Code:
paste -sd"  \n" $InFile >$OutFile
Daniel B. Martin
can you please provide explanation of this command "paste -sd" is working?

Thanks!!!
 
Old 09-17-2014, 08:22 PM   #5
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,670

Rep: Reputation: 542Reputation: 542Reputation: 542Reputation: 542Reputation: 542Reputation: 542
Quote:
Originally Posted by jone kim View Post
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.

Daniel B. Martin
 
Old 09-18-2014, 02:21 AM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,619

Rep: Reputation: 2940Reputation: 2940Reputation: 2940Reputation: 2940Reputation: 2940Reputation: 2940Reputation: 2940Reputation: 2940Reputation: 2940Reputation: 2940Reputation: 2940
How about:
Code:
awk 'NF && ORS=(/[0-9]/)?RS:" "' file
 
Old 09-18-2014, 02:24 AM   #7
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 16,976

Rep: Reputation: 2565Reputation: 2565Reputation: 2565Reputation: 2565Reputation: 2565Reputation: 2565Reputation: 2565Reputation: 2565Reputation: 2565Reputation: 2565Reputation: 2565
Code:
awk '{gsub(/\n/, " ")}1' RS='' names.txt
???
 
Old 09-18-2014, 03:23 AM   #8
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,619

Rep: Reputation: 2940Reputation: 2940Reputation: 2940Reputation: 2940Reputation: 2940Reputation: 2940Reputation: 2940Reputation: 2940Reputation: 2940Reputation: 2940Reputation: 2940
Well of course you could do that ... sheesh .. hehe

Also:
Code:
awk 'gsub(/\n/, "|")' RS='' file

awk '$1=$1' RS='' FS='\n' OFS=' ' file
 
Old 09-18-2014, 03:35 AM   #9
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 11,283

Rep: Reputation: 3390Reputation: 3390Reputation: 3390Reputation: 3390Reputation: 3390Reputation: 3390Reputation: 3390Reputation: 3390Reputation: 3390Reputation: 3390Reputation: 3390
what about sed?
sed 'h;N;N;s/\n/ /g'
 
1 members found this post helpful.
Old 09-18-2014, 07:15 AM   #10
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,670

Rep: Reputation: 542Reputation: 542Reputation: 542Reputation: 542Reputation: 542Reputation: 542
Quote:
Originally Posted by pan64 View Post
what about sed?
sed 'h;N;N;s/\n/ /g'
Nice!

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
Daniel B. Martin
 
Old 09-18-2014, 09:37 AM   #11
firstfire
Member
 
Registered: Mar 2006
Location: Ekaterinburg, Russia
Distribution: Debian, Ubuntu
Posts: 709

Rep: Reputation: 427Reputation: 427Reputation: 427Reputation: 427Reputation: 427
Hi.

Here is the solution using ed text editor:
Code:
/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.
 
  


Reply

Tags
awk, sed


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] script to reorder data in a row rebelbuttmunch Programming 19 05-01-2014 11:36 AM
display a row of data from a database in a table format 3vra Programming 2 03-13-2009 10:25 AM
how to get the second column and first row data from a file??? loplayers Linux - Newbie 3 11-05-2007 07:35 PM
mysql alter row data kpachopoulos Programming 8 09-26-2007 02:54 AM
Painting the row of a data grid a color in VB.net mrobertson Programming 1 07-30-2005 04:22 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:33 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration