Simple script to write new lines in a text file


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Simple script to write new lines in a text file
# 1  
Old 05-26-2012
Simple script to write new lines in a text file

Hello, I have a comma seperated data sheet with multiple fields of biological data. One column contains the ID name of the sample, where there could be more than one sample separated by a comma. I would like a script that reads this field, and for each sample ID, copies the entire line and writes it below.

For example:

INPUT
Code:
Col1    Col2    Col3                Col4     Col5
data1   data7   NAME1              data11   data15
data2   data8   NAME2              data12   data16
data3   data9   NAME3,NAME4        data13   data17
data4   data10  NAME5              data14   data18
data19  data20  NAME6,NAME7,NAME8  data21   data22

Col3 = sample column (contains up to 15 samples, separated by comma)

OUTPUT
Code:
Col1    Col2    Col3           Col4      Col5
data1   data7   NAME1         data11   data15
data2   data8   NAME2         data12   data16
data3   data9   NAME3         data13   data17
data3   data9   NAME4         data13   data17
data4   data10  NAME5         data14   data18
data19  data20  NAME6         data21   data22
data19  data20  NAME7         data21   data22
data19  data20  NAME8         data21   data22

Any help appreciated!!!
Thanks!
# 2  
Old 05-26-2012
If you don't mind losing spacing:
Code:
awk '{n=split($3,a,",");for (i=1;i<=n;i++) {$3=a[i];print}}' file

# 3  
Old 05-26-2012
Quote:
Originally Posted by bartus11
If you don't mind losing spacing:
Code:
awk '{n=split($3,a,",");for (i=1;i<=n;i++) {$3=a[i];print}}' file

Thanks for your reply!!
What does that mean? Losing what spacing?
# 4  
Old 05-26-2012
Try that code on your sample data and you will see... Smilie
# 5  
Old 05-26-2012
Quote:
Originally Posted by bartus11
Try that code on your sample data and you will see... Smilie
Whoa...I'm afraid that doesn't help much Smilie There is only 1 field now and I don't think it copied and reprinted the new lines.
...what if I change to tab delimited? Or if I change the sample delimiter to a different character?
# 6  
Old 05-26-2012
Example that preserves space:
Code:
awk 'n=split($3,T,","){for (i=1;i<=n;i++) {p=$0; sub($3,sprintf("%-" length($3) "s",T[i]),p);print p}next}1' infile


--
On Solaris use /usr/xpg4/bin/awk rather than awk

Last edited by Scrutinizer; 05-26-2012 at 05:59 PM..
# 7  
Old 05-26-2012
This is what I get:
Code:
# cat file
Col1    Col2    Col3                Col4     Col5
data1   data7   NAME1              data11   data15
data2   data8   NAME2              data12   data16
data3   data9   NAME3,NAME4        data13   data17
data4   data10  NAME5              data14   data18
data19  data20  NAME6,NAME7,NAME8  data21   data22
# awk '{n=split($3,a,",");for (i=1;i<=n;i++) {$3=a[i];print}}' file
Col1 Col2 Col3 Col4 Col5
data1 data7 NAME1 data11 data15
data2 data8 NAME2 data12 data16
data3 data9 NAME3 data13 data17
data3 data9 NAME4 data13 data17
data4 data10 NAME5 data14 data18
data19 data20 NAME6 data21 data22
data19 data20 NAME7 data21 data22
data19 data20 NAME8 data21 data22

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Match text to lines in a file, iterate backwards until text or text substring matches, print to file

hi all, trying this using shell/bash with sed/awk/grep I have two files, one containing one column, the other containing multiple columns (comma delimited). file1.txt abc12345 def12345 ghi54321 ... file2.txt abc1,text1,texta abc,text2,textb def123,text3,textc gh,text4,textd... (6 Replies)
Discussion started by: shogun1970
6 Replies

2. Shell Programming and Scripting

Simple script to test mountGpoint for read-write

Guys, need your help urgently. ServerA ServerB ServerA has an email facility and can connect remotely via root using key to ServerB What I am trying to achieve here is ServerA would be able to remotely: 1. Do command 'touch /mnt/testfile.date' on ServerB 2. If touch failed with... (4 Replies)
Discussion started by: jaapar
4 Replies

3. Shell Programming and Scripting

Need help to write a shell script to convert text file to excel file.

Hi Everyone, I want your help to write a script which will take text file as input and on the basis of delimiter ":"script will create excel sheet. Example input: IpAdress:InstanceName:Port:ServerName 10.255.255.1:abc:2232:xyz_abc Output should be an excel sheet like below: Column... (8 Replies)
Discussion started by: akabhinav18
8 Replies

4. Shell Programming and Scripting

Bash script - printing range of lines from text file

I'm working on a new exercise that calls for a script that will take in two arguments on the command line (representing the range of line numbers) and will subsequently print those lines from a a specified file. Command line would look like this: ./lines_script.bash 5 15 <file.txt. The script would... (8 Replies)
Discussion started by: ksmarine1980
8 Replies

5. Shell Programming and Scripting

How to write text file data to excel using UNIX shell script?

Hi All, I have the requirement in unix shell script. I want to write the "ls -ltr" command out put to excel file as below. Input :text file data : drwxr-xr-x 5 root root 4096 Oct 2 12:26 drwxr-xr-x 2 apx aim 4096 Nov 29 18:40 drwxr-xr-x 5 root root 4096 Oct 2 12:26 drwxr-xr-x... (10 Replies)
Discussion started by: Balasankar
10 Replies

6. Shell Programming and Scripting

how to write multiple lines to a file using shell script?

I need to create an xml using shell script, but i first want to know how can i write multiple lines to file using shell script? (7 Replies)
Discussion started by: vel4ever
7 Replies

7. Shell Programming and Scripting

How to write a script for text file encryption?

Hi All, I have zero knowledge in UNIX, so i desperately need the help from you guys. I need a script which can encrypt all the text files in one shot in a source directory. After all the text files are encrypted, they will be put into a separate folder. Command involve: gpg with -a,... (6 Replies)
Discussion started by: shanbalao
6 Replies

8. Shell Programming and Scripting

looking for a script that will delete lines in a text file

it will grep for a line and then delete these line. how do i begin to write this script if theres no available one? (3 Replies)
Discussion started by: garfish
3 Replies

9. Shell Programming and Scripting

how to write shell script to extract lines we want

hi i have a file which is very large . it contains lines in the format below: seed url, html url .... ... seed url, html url i have sort it already. 2010ÄÏ·ÇÊÀ½ç±*_¾º¼¼·ç±©_ÐÂÀËÍø ÕżªÁúרÀ¸£ºÊÀ½ç±*24ÄêµÄ»ØÒä ÆÚÅÎÑÇÖÞδÀ´ÍŽá_2010ÄÏ·ÇÊÀ½ç±*_¾º¼¼·ç±©_ÐÂÀËÍø 2010ÄÏ·ÇÊÀ½ç±*_¾º¼¼·ç±©_ÐÂÀËÍø ¹úÃ×Óë±´ÄáÌØ˹´ï³ÉÐ*Òé... (6 Replies)
Discussion started by: rainboisterous
6 Replies

10. Shell Programming and Scripting

Shell script to read lines in a text file and filter user data

hi all, I have this file with some user data. example: $cat myfile.txt FName|LName|Gender|Company|Branch|Bday|Salary|Age aaaa|bbbb|male|cccc|dddd|19900814|15000|20| eeee|asdg|male|gggg|ksgu|19911216||| aara|bdbm|male|kkkk|acke|19931018||23| asad|kfjg|male|kkkc|gkgg|19921213|14000|24|... (4 Replies)
Discussion started by: srimal
4 Replies
Login or Register to Ask a Question