LinuxQuestions.org
Review your favorite Linux distribution.
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 10-07-2013, 02:47 AM   #1
sryzdn
Member
 
Registered: Feb 2013
Posts: 248

Rep: Reputation: 1
how to insert a tab between columns


Hi,

I have a file like this:

Quote:
-0.0000164102 1.8761088848 1.7963805199
1.6247659935 0.9380401324 4.3998305798
-1.6247665923 4.6902583711 1.7963805199
0.0000158113 3.7521896186 4.3998305798
-1.6247343708 6.5663391049 4.3998305798
I want to put a tab between each column and make them come down straightly. Please let me know which script can do it for me?
 
Old 10-07-2013, 02:49 AM   #2
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 8,110

Rep: Reputation: 2268Reputation: 2268Reputation: 2268Reputation: 2268Reputation: 2268Reputation: 2268Reputation: 2268Reputation: 2268Reputation: 2268Reputation: 2268Reputation: 2268
what is now there (I assume space)?
You can try:
sed 's/ /\t/g' infile > outfile
 
1 members found this post helpful.
Old 10-07-2013, 03:25 AM   #3
sryzdn
Member
 
Registered: Feb 2013
Posts: 248

Original Poster
Rep: Reputation: 1
Quote:
Originally Posted by pan64 View Post
what is now there (I assume space)?
You can try:
sed 's/ /\t/g' infile > outfile
Great, thank you.

Is there a way that I can add the row number to the right side of these columns as well with a "tab" distance? like this:

Quote:
-0.0000163317 1.8761090040 0.0000000000 1
1.6247660720 0.9380402852 2.6034500599 2
-1.6247665138 4.6902584903 0.0000000000 3
0.0000158898 3.7521897714 2.6034500599 4
-1.6247342923 6.5663392577 2.6034500599 5
 
Old 10-07-2013, 03:39 AM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
Have a look at this:
Code:
awk 'BEGIN{ OFS="\t"}{ print $1, $2, $3, NR }' infile
This will result in the following output:
Code:
-0.0000164102   1.8761088848    1.7963805199    1
1.6247659935    0.9380401324    4.3998305798    2
-1.6247665923   4.6902583711    1.7963805199    3
0.0000158113    3.7521896186    4.3998305798    4
-1.6247343708   6.5663391049    4.3998305798    5
Have a look her for an explanation of the OFS, NR variable (and others):
- Awk Built-in Variables – FS, OFS, RS, ORS, NR, NF, FILENAME, FNR
 
1 members found this post helpful.
Old 10-07-2013, 03:48 AM   #5
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 8,110

Rep: Reputation: 2268Reputation: 2268Reputation: 2268Reputation: 2268Reputation: 2268Reputation: 2268Reputation: 2268Reputation: 2268Reputation: 2268Reputation: 2268Reputation: 2268
hm, that can be made with sed, but probably easier with awk or perl:

perl -ne '{s! !\t!g, s![\n\r]!!g; $a++; print "$_\t$a\n" } '
 
1 members found this post helpful.
Old 10-07-2013, 03:52 AM   #6
sryzdn
Member
 
Registered: Feb 2013
Posts: 248

Original Poster
Rep: Reputation: 1
Thanks for the very great post. I tried to add "ZZ" in the middle but it failed. Is there way I can do as follows:

Code:
-0.0000164102   1.8761088848    1.7963805199   ZZ   1
1.6247659935    0.9380401324    4.3998305798   ZZ   2
-1.6247665923   4.6902583711    1.7963805199   ZZ   3
0.0000158113    3.7521896186    4.3998305798   ZZ   4
-1.6247343708   6.5663391049    4.3998305798   ZZ   5
 
Old 10-07-2013, 03:54 AM   #7
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
Well, ZZ is a string, therefore you have to embed it in double quotes:
Code:
awk 'BEGIN{OFS="\t"}{print $1, $2, $3, "ZZ", NR}' infile
 
1 members found this post helpful.
Old 10-07-2013, 03:57 AM   #8
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 8,110

Rep: Reputation: 2268Reputation: 2268Reputation: 2268Reputation: 2268Reputation: 2268Reputation: 2268Reputation: 2268Reputation: 2268Reputation: 2268Reputation: 2268Reputation: 2268
perl -ne '{s! !\t!g, s![\n\r]!!g; $a++; print "$_\tZZ\t$a\n" } ' infile
 
1 members found this post helpful.
Old 10-07-2013, 08:46 AM   #9
allend
Senior Member
 
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware-current
Posts: 4,430

Rep: Reputation: 1350Reputation: 1350Reputation: 1350Reputation: 1350Reputation: 1350Reputation: 1350Reputation: 1350Reputation: 1350Reputation: 1350Reputation: 1350
Just for fun, a bash solution.
Code:
 i=1;while read; do echo -e "${REPLY// /\t}\tZZ\t$i";(( i+=1 )); done < infile
 
  


Reply


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] Insert column with awk or sed between two columns captainentropy Linux - Newbie 9 11-27-2014 12:49 PM
[SOLVED] Insert tab using echo Vthimmap Linux - Newbie 3 11-27-2012 01:53 AM
OO writer: I need to insert columns from a certain point newbiesforever Linux - Software 2 10-07-2009 08:35 PM
convert columns to rows (tab separated file to csv) doug23 Programming 16 08-16-2009 10:14 PM
sed command to insert tab? iluvatar Linux - Software 4 06-29-2004 02:56 AM


All times are GMT -5. The time now is 08:30 PM.

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