parse file into tab separated columns


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting parse file into tab separated columns
# 1  
Old 06-03-2009
parse file into tab separated columns

Hello,
I am trying to parse a file that resembles the last three groupings into something looking like the first two lines. I've fiddled with sed and awk a bit, but can't get anything to work properly. I need them separated by some delimiter. The file is some 23,000 lines of the stuff....

Thanks!

EDIT: In Bash environment

Code:
5   8.001   0   0.000   5.000
10  8.001   0   0.000   10.000

15
8.001
0
0.000
15.000

20
8.001
0
0.000
20.000

25
8.001
0
0.000
25.000


Last edited by dkozel; 06-03-2009 at 01:58 PM..
# 2  
Old 06-03-2009
Code:
awk '{if(length($0)> 0)  {printf("%s\t",$0)} else {print $0}' infile > outfile

Try that.
# 3  
Old 06-03-2009
Solution Found

Jim: Thanks for the reply, I'll try that as well.

The solution I found was to open vim and:

Replaced newline characters with tabs
:%s/\n/^I/g

Then replaced pairs of tabs with a newline (the space between sets generated a pair)
:%s/^I^I/\r/g

Thanks again. This site is an awesome resource.
# 4  
Old 06-03-2009
Code:
awk '{for(i=1;i<=NF;i++)print $i;printf "\n"}' file

# 5  
Old 06-03-2009
I couldn't get either 1-liner above to work but here's a script that I could get to work:

Code:
#!/bin/bash

count=0

for i in `cat sortdata | awk '/./'`; do
  echo -ne "$i\t"
  count=$((count+1))
  if [ $count -eq "5" ]; then
    echo ""
    count=0
  fi
done

In this case "sortdata" is a file that looks like:

Code:
[a.mjglenne ~]$ cat sortdata
15
8.001
0
0.000
15.000

20
8.001
0
0.000
20.000

25
8.001
0
0.000
25.000

And it runs like:

Code:
[a.mjglenne ~]$ ./sort.sh
15      8.001   0       0.000   15.000
20      8.001   0       0.000   20.000
25      8.001   0       0.000   25.000

# 6  
Old 06-03-2009
My assumption of your i/p and op/ were vice-versa.
Try this:

Code:
awk 'BEGIN{FS="\n";RS="\n\n"}NF>0{for(i=1;i<=NF;i++)printf("%s\t",$i);printf "\n"}' file

HTML Code:
15      8.001   0       0.000   15.000
20      8.001   0       0.000   20.000
25      8.001   0       0.000   25.000

-Devaraj Takhellambam

Last edited by devtakh; 06-03-2009 at 03:17 PM.. Reason: wrap tag
# 7  
Old 06-03-2009
Using perl:

Code:
$
$ cat data.txt
15
8.001
0
0.000
15.000

20
8.001
0
0.000
20.000

25
8.001
0
0.000
25.000
$
$ perl -ne 'chomp; if (/^$/) {print substr($buf,1),"\n"; $buf=""} else {$buf.="\t".$_}
>   END {print substr($buf,1)."\n"}' data.txt
15      8.001   0       0.000   15.000
20      8.001   0       0.000   20.000
25      8.001   0       0.000   25.000
$
$

tyler_durden
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

awk to parse current and next row in tab-delimited file

Hi there, I would like to use awk to reformat a tab-delimited file containing three columns as follows: Data file: sample 1 173 sample 269 530 sample 687 733 sample 1699 1779 Desired output file: sample 174..265, 531..686, 734..1698 I need the value... (5 Replies)
Discussion started by: emiley
5 Replies

2. Shell Programming and Scripting

Read a tab separated file with empty column

Hi all, I'm trying to read a tab separated file and apply some functions on each column. I have an issue with empty column. Exemple: $ #cat with the sed to allow you to see my tab $ cat foo.txt| sed 's/\t/;/g' a;1;x b;;yI wanted to something like that: while read col1 col2 col3 do ... (4 Replies)
Discussion started by: maturix
4 Replies

3. Shell Programming and Scripting

How to replace & with and in tab separated file?

Hi, I have a tab separated. I want to replace all the "&" in 8th column of the file with "and" .I am trying with awk -F, -vOFS=\\t '{$8=($8=="&")?"and":$8}1' test> test1.txt My file is abc def ghk hjk lkm hgb jkluy acvf & bhj hihuhu fgg me mine he her go went has has & had hgf hgy ... (1 Reply)
Discussion started by: jagdishrout
1 Replies

4. Shell Programming and Scripting

Problem with a tab separated file

Hi, I have created a tab separated file from the following input file. ADDRESS1 CITY STATE POSTAL COUNTRY LON LAT 32 PRINZREGENTENSTRASSE ROSENHEIM BAYERN 83022 DEU 1212182 4785699 263 VIA DANTE ALIGHIERI BARI PUGLIA 70122 ITA 1686233 4112154 30 VIA MILANO ... (1 Reply)
Discussion started by: ramky79
1 Replies

5. UNIX for Dummies Questions & Answers

Filling a tab-separated file with known missing entries in columns

Hello all, I have a file which is tab separated like that: PHE_205_A TIP_127_W ARG_150_B MET_1150_A TIP_12_W VAL_11_B GLU_60_A TIP_130_W ARG_143_B LEU_1033_A TIP_203_W ARG_14_B SER_1092_A TIP_203_W THR_1090_A TIP_203_W SER_1092_A TIP_25_W ... (6 Replies)
Discussion started by: TheTransporter
6 Replies

6. UNIX for Dummies Questions & Answers

tab-separated file to matrix conversion

hello all, i have an input file like that A A X0 A B X1 A C X2 ... A Z Xx B A X1 B B X3 .... Z A Xx Z B X4 and i want to have an output like that A B C D A X0 X1 X2 Xy B X1 X3 X4 (4 Replies)
Discussion started by: TheTransporter
4 Replies

7. Shell Programming and Scripting

Convert a tab separated file using bash

Dear all, I have a file in this format (like a matrix) - A B C .. X A 1 4 2 .. 2 B 2 6 4 .. 8 C 3 5 5 .. 4 . . . ... . X . . ... . and want to convert it into a file with this format: A A = 1 A B = 4 A C = 2 ... A X = 2 B A = 2 B B = 6 etc (2 Replies)
Discussion started by: TheTransporter
2 Replies

8. Shell Programming and Scripting

Compare two columns separated by a tab

witam potrzebuje polecenia porownujacego koumny na podstawie n-ostatnich znakow danej linnijki tj mam 2 koumny AiB zawierajace ciag dowolnych znakow (dlugosci w kazdej linijce mga byc rozne wiec uzycie substra odpada) A B ewewewabc nbgujnnabc... (3 Replies)
Discussion started by: Toudi
3 Replies

9. UNIX for Dummies Questions & Answers

Sum up a decimal column in a tab separated text file and error handling

Hi, I have a small requirement where i need to sum up a column in a text file. Input file 66ab 000000 534385 -00000106350.00 66cd 000000 534485 -00013364511.00 66ad 000000 534485 -00000426548.00 672a 000000 534485 000000650339.82... (5 Replies)
Discussion started by: pssandeep
5 Replies

10. Shell Programming and Scripting

Unix shell script to parse the contents of comma-separated file

Dear All, I have a comma-separated file. 1. The first line of the file(header) should have 4 commas(5 fields). 2. The last line of the file should have 1 comma(2 fields). Pls help me in checking this condition in a shell script. And the number of lines between the first line and last... (11 Replies)
Discussion started by: KrishnaSaran
11 Replies
Login or Register to Ask a Question