Split each column in TSV file to be new line?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Split each column in TSV file to be new line?
# 1  
Old 01-23-2013
Split each column in TSV file to be new line?

My TSV looks like:

Code:
Hello my name is John \t Hello world \t Have a good day! \t See you later!

Is there a simple bash script that splits the tsv on tab to:
Hello my name is John
Hello world
Have a good day!
See you later!

I'm really stuck, would appreciate any help!
# 2  
Old 01-23-2013
Code:
tr '\t' '\n' < filename

This User Gave Thanks to Yoda For This Post:
# 3  
Old 01-23-2013
Code:
sed 's/<press tab here>/\n/g' file

this should help Smilie
# 4  
Old 01-23-2013
Quote:
Originally Posted by bipinajith
Code:
tr '\t' '\n' < filename

This works! I'm adding this into an earlier script that takes lines from a tsv. Does anyone know how to simplify this? Is tmp.txt even necessary?

sed -n 13,16p input.tsv > tmp.txt | tr '\t' '\n' < tmp.txt > output1.txt
sed -n 17,20p input.tsv > tmp.txt | tr '\t' '\n' < tmp.txt > output2.txt
sed -n 21,25p input.tsv > tmp.txt | tr '\t' '\n' < tmp.txt > output3.txt
# 5  
Old 01-23-2013
what is your original .tsv file?? why are you taking up lines like these
Code:
sed -n 13,16p input.tsv > tmp.txt | tr '\t' '\n' < tmp.txt > output1.txt 
sed -n 17,20p input.tsv > tmp.txt | tr '\t' '\n' < tmp.txt > output2.txt
sed -n 21,25p input.tsv > tmp.txt | tr '\t' '\n' < tmp.txt > output3.txt

Instead, you could have used

Code:
sed -n 13,25p input.tsv | tr '\t' '\n' > output1.txt

# 6  
Old 01-23-2013
Or use awk
Code:
awk '{
 gsub(/\t/,"\n");
}NR>=13&&NR<=16 {
 print $0 > "output1.txt";
}NR>=17&&NR<=20 {
 print $0 > "output2.txt";
}NR>=21&&NR<=25 {
 print $0 > "output3.txt";
}' input.tsv

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to split a file into column with awk?

The following is my code nawk -F',' ' BEGIN { printf "MSISDN,IMSI,NAM,TS11,TS21,TS22,OBO,OBI,BAIC,BAOC,BOIC,BOIEXH,APNID0,APNID1,APNID2,APNID3,APNID0,CSP,RSA\n" } { for(i=1; i<=NF; i++) { split($i,a,":") gsub(" ","", a) printf "%s;",a } printf "\n" }'HLR_DUMP_BZV >> HLR_full This is... (1 Reply)
Discussion started by: gillesi
1 Replies

2. Shell Programming and Scripting

Split file by column value, each with header

Hello all, I have a csv with with different testcase values in column 5. year,min,max,Instrument,Testcase 2016,201,1003,GEOTROPH-02116,TATA7980 2016,53,1011,GEOTROPH-01963,TATA7980 2016,3,1024,GEOTROPH-02067,TATA7980 2016,203,1027,GEOTROPH-02011,TATA7980... (16 Replies)
Discussion started by: senhia83
16 Replies

3. Shell Programming and Scripting

Split certain strings in a line for a specific column.

Hi, i need help to extract certain strings/words from lines with different length. I have 3 columns separated by tab delimiter. like below Probable arabinan endo-1,5-alpha-L-arabinosidase A (EC 3.2.1.99) (Endo-1,5-alpha-L-arabinanase A) (ABN A) abnA Ady3G14620 Probable arabinan... (5 Replies)
Discussion started by: redse171
5 Replies

4. Shell Programming and Scripting

Split file according to column values

Hi all, I am trying to split a file by the values of the FIRST column. The following awk works to split the file by the value of the LAST column -- How can I alter this to divide the column by the FIRST column?? awk -F"\t" '{ print > $NF ; close($NF)}' filename1Thanks! (5 Replies)
Discussion started by: owwow14
5 Replies

5. UNIX for Dummies Questions & Answers

Split file based on column

i have file1.txt asdas|csada|130310|0423|A1|canberra sdasd|sfdsf|130426|2328|A1|sydney Expected output : on eaceh third and fourth colum, split into each two characters asdas|csada|13|03|10|04|23|A1|canberra sdasd|sfdsf|13|04|26|23|28|A1|sydney (10 Replies)
Discussion started by: radius
10 Replies

6. Shell Programming and Scripting

Split file by column value

Please help me figure out whats wrong with my code, I have to split a file according to values in 3rd thru 6th column, If the 3rd or 4th col values are >0, row goes to 1 file, if 3rd and 5th are >0,row goes to another file...and so on... What is wrong with my code? It doesn't seem to work.... (2 Replies)
Discussion started by: newbie83
2 Replies

7. Shell Programming and Scripting

Split a file into multiple files based on line numbers and first column value

Hi All I have one query,say i have a requirement like the below code should be move to diffent files whose maximum lines can be of 10 lines.Say in the below example,it consist of 14 lines. This should be moved logically using the data in the fisrt coloumn to file1 and file 2.The data of first... (2 Replies)
Discussion started by: sarav.shan
2 Replies

8. Shell Programming and Scripting

On the command line using bash, how do you split a string by column?

Input: MD5(secret.txt)= fe66cbf9d929934b09cc7e8be890522e MD5(secret2.txt)= asd123qwlkjgre5ug8je7hlt488dkr0p I want the results to look like these, respectively: MD5(secret.txt)= fe66cbf9 d929934b 09cc7e8b e890522e MD5(secret2.txt)= asd123qw lkjgre5u g8je7hlt 488dkr0p Basically, keeping... (11 Replies)
Discussion started by: teiji
11 Replies

9. UNIX for Dummies Questions & Answers

tsv file comparison

Hi Guys, I have to write a script to compare the tsv files headers with the values stored in the variable in bash. I have declared variables as HEADER_VERSION="All Versions\tVersion Type\tVersion\tLevel" now file contains data as follows: All Versions Version Type Version Level... (0 Replies)
Discussion started by: Swapna173
0 Replies

10. Shell Programming and Scripting

4 column tsv file, output 1 specific column

Hello all siteexplorer.search.yahoo.com can output results in tsv format, when opened in excel I get 4 columns. I would like to wget that file, which I can do. I would then like to pull the 2nd column and output it only. I've searched around and found a few bits and pieces but nothing I've... (6 Replies)
Discussion started by: casphar
6 Replies
Login or Register to Ask a Question