LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 06-25-2013, 07:33 AM   #1
hoi
Member
 
Registered: Jun 2013
Posts: 32

Rep: Reputation: Disabled
splitting a coloum in row


I have data file like

1
2
3
4
5
6
7
8
9
10

I want to write like

1 2
3 4
5 6
7 8
9 10

How do I make it by 'awk command?
 
Old 06-25-2013, 08:03 AM   #2
Madhu Desai
Member
 
Registered: Mar 2013
Distribution: Rocky, Fedora, Ubuntu
Posts: 541

Rep: Reputation: 153Reputation: 153
Code:
$ awk '{printf("%s",NR%2 ? $0" ":$0"\n")}' file
Or simply you can use
Code:
$ paste - - < file

## To put space instead of tab
$ paste -d' ' - - < file

Last edited by Madhu Desai; 06-25-2013 at 08:11 AM.
 
1 members found this post helpful.
Old 06-25-2013, 09:33 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,006

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Code:
awk 'ORS = NR % 2?" ":"\n"' file
 
2 members found this post helpful.
Old 06-25-2013, 04:06 PM   #4
linuxCode
Member
 
Registered: Jun 2013
Location: New York City
Distribution: Arch
Posts: 33

Rep: Reputation: 44
Code:
cat file | xargs -n2
Sorry, my mistake. you wanted awk

Last edited by linuxCode; 06-25-2013 at 07:10 PM.
 
2 members found this post helpful.
Old 06-26-2013, 12:23 AM   #5
hoi
Member
 
Registered: Jun 2013
Posts: 32

Original Poster
Rep: Reputation: Disabled
Thanks to all
really all answer are helpful for my work
again thanks to all
 
Old 06-26-2013, 12:29 AM   #6
hoi
Member
 
Registered: Jun 2013
Posts: 32

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by linuxCode View Post
Code:
cat file | xargs -n2
Sorry, my mistake. you wanted awk
Thanks
xargs, it was not known to me
Now I am using xargs
thank you
 
Old 06-26-2013, 12:31 AM   #7
hoi
Member
 
Registered: Jun 2013
Posts: 32

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by mddesai View Post
Code:
$ awk '{printf("%s",NR%2 ? $0" ":$0"\n")}' file
Or simply you can use
Code:
$ paste - - < file

## To put space instead of tab
$ paste -d' ' - - < file
Now I have understand that paste is better than awk in this case
Thank you
 
Old 06-26-2013, 12:47 AM   #8
linuxCode
Member
 
Registered: Jun 2013
Location: New York City
Distribution: Arch
Posts: 33

Rep: Reputation: 44
Quote:
Originally Posted by hoi View Post
Thanks
xargs, it was not known to me
Now I am using xargs
thank you
You're welcome

BTW, the command I gave you doesn't modify your original file. Your original file will be the same. If you want the new changes redirect the output to another file. For example

Code:
cat file | xargs -n2 > newfile
Welcome to LQ!

Last edited by linuxCode; 06-26-2013 at 12:51 AM.
 
Old 06-28-2013, 05:22 AM   #9
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
I must say I like the xargs solution. Clever. I'd skip cat though and use a shell redirection.

Code:
xargs -n2 <infile.txt
However, here's yet another option for you:

Code:
sed 'N;s/\n/ /' infile.txt
You can concatenate 3 or more lines together with additional N commands and the g flag to the substitution. And the -i option (assuming gnu sed) can be used to return the edits directly to the input file.

Code:
sed -i 'N;N;s/\n/ /g' infile.txt
Finally, here's a quick bash-only solution that will work as long as the input isn't too large and you're using v.4+. It should be very fast on small files.

Code:
mapfile -t nums <infile.txt
printf '%s %s\n' "${nums[@]}"

Last edited by David the H.; 06-28-2013 at 05:25 AM.
 
  


Reply



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
deleting every other row, sometimes atjurhs Linux - Newbie 10 09-28-2012 11:07 AM
[SOLVED] find a null value in a row/column and delete entire row umix Linux - Newbie 10 10-13-2011 01:26 AM
gnome-terminal is missing one pixel row and its an important row rednuht Linux - General 1 12-24-2009 10:30 AM
Row manipulation with awk SHIFTA Linux - Newbie 1 11-05-2009 10:37 PM
Shell script to parse csv-like output, row by row utahnix Linux - General 8 12-08-2007 05:03 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 12:37 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
Open Source Consulting | Domain Registration