An easier way to move character from field to a new line


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers An easier way to move character from field to a new line
# 1  
Old 03-06-2012
An easier way to move character from field to a new line

Hi !

Is there a more convenient way to do the following:
When, in $2, there is more then one letter (separated by ","), it returns the letter on the second position to a new line in $2 with the same content in $1 and $3 as the line this letter comes from.

infile:
Code:
aaaaa   A,B   1,2,3,4,5
bbbb    C       6,7,8
ccccc   D,E    9

outfile:
Code:
aaaaa   A   1,2,3,4,5
aaaaa   B   1,2,3,4,5
bbbb    C   6,7,8
ccccc   D   9
ccccc   E   9

The only (very amateurish) way I found is:
Code:
awk -F'\t' 'BEGIN{OFS="\t"} {gsub(/,/,"\t",$2); print}' infile | cut -f 1,3,4 > outfile1
awk -F'\t' 'BEGIN{OFS="\t"} {gsub(/,/,"\t",$2); print}' infile | cut -f 1,2,4 > outfile2
cat outfile1 outfile2

# 2  
Old 03-06-2012
Try:
Code:
awk -F"\t" -vOFS="\t" '{n=split($2,a,",");for (i=1;i<=n;i++) print $1,a[i],$3}' file

This User Gave Thanks to bartus11 For This Post:
# 3  
Old 03-06-2012
Code:
perl -lane '@x=split /,/,$F[1]; print "$F[0]\t$_\t$F[2]" for(@x)' inputfile

This User Gave Thanks to balajesuri For This Post:
# 4  
Old 03-06-2012
Thanks for your help guys !

@ bartus11:
I think there is a typo (missing space after "-v"):
Code:
awk -F"\t" -v OFS="\t" '{n=split($2,a,",");for (i=1;i<=n;i++) print $1,a[i],$3}' file

But it still returns an empty file. What is the meaning of "i++" in the statement?

@ balajesuri:
It works on the example.
But, sometimes as I can have text separated by space in $1, how do you do to mention that fields are tab-delimited in perl (like -F"\t" in awk) ?
# 5  
Old 03-06-2012
You're correct about the -v typo.
Quote:
Originally Posted by lucasvs
But it still returns an empty file.
It doesn't return any file, unless you happen to redirect into one...
Quote:
What is the meaning of "i++" in the statement?
It's a short form of "i=i+1". It's just a normal C-style for loop.
# 6  
Old 03-06-2012
Sorry bartus11, my mistake, your code works very well !
It is just that I put spaces in the example instead of tab to be more readable.

---------- Post updated at 07:24 PM ---------- Previous update was at 07:23 PM ----------

Thanks Corona688 for the explanation !

It's clear for me now.
# 7  
Old 03-06-2012
Quote:
Originally Posted by lucasvs
@ balajesuri:
It works on the example.
But, sometimes as I can have text separated by space in $1, how do you do to mention that fields are tab-delimited in perl (like -F"\t" in awk) ?
Yes, you can start with perl -F'\t' -lane '.....
This User Gave Thanks to balajesuri For This Post:
 
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 repeat a character in a field if it's a single character?

I have a csv dataset like this : C,rs18768 G,rs13785 GA,rs1065 G,rs1801279 T,rs9274407 A,rs730012 I'm thinking of use like awk, sed to covert the dataset to this format: (if it's two character, then keep the same) CC,rs18768 GG,rs13785 GA,rs1065 GG,rs1801279 TT,rs9274407... (7 Replies)
Discussion started by: nengcheng
7 Replies

2. Shell Programming and Scripting

Command/script to match a field and print the next field of each line in a file.

Hello, I have a text file in the below format: Source Destination State Lag Status CQA02W2K12pl:D:\CAQA ... (10 Replies)
Discussion started by: pocodot
10 Replies

3. Shell Programming and Scripting

URGENT!!!move character x of input string to some position

i have this prob I have some records in a file1 like this 1001 sajal singh tampa 1002 .... so on i have a pattern file which is like this 1,4 4,13 14,15 i have to read the first pair 1,4 and extract that from the first record so the pattern is 1001 now i have to scramble 1001... (1 Reply)
Discussion started by: spankincubus
1 Replies

4. Shell Programming and Scripting

Replace a field with a character as per the field length

Hi all, I have a requirement to replace a field with a character as per the length of the field. Suppose i have a file where second field is of 20 character length. I want to replace second field with 20 stars (*). like ******************** As the field is not a fixed one, i want to do the... (2 Replies)
Discussion started by: gani_85
2 Replies

5. Shell Programming and Scripting

Compare Field in Current Line with Field in Previous

Hi Guys I have the following file Essentially, I am trying to find the right awk/sed syntax in order to produce the following 3 distinct files from the file above: Basically, I want to print the lines of the file as long as the second field of the current line is equal to the... (9 Replies)
Discussion started by: moutaye
9 Replies

6. HP-UX

How to remove new line character and append new line character in a file?

Hi Experts, I have data coming in 4 columns and there are new line characters \n in between the data. I need to remove the new line characters in the middle of the row and keep the \n character at the end of the line. File is comma (,) seperated. Eg: ID,Client ,SNo,Rank 37,Airtel \n... (8 Replies)
Discussion started by: sasikari
8 Replies

7. Shell Programming and Scripting

Count number of occurences of a character in a field defined by the character in another field

Hello, I have a text file with n lines in the following format (9 column fields): Example: contig00012 149606 G C 49 68 60 18 c$cccccacccccccccc^c I need to count the number of lower-case and upper-case occurences in column 9, respectively, of the... (3 Replies)
Discussion started by: s052866
3 Replies

8. Shell Programming and Scripting

How to append a character to the last but one field on a specific line?

Hi Guys, I have a file like this: aaa b c d e f fsss g h i k l qqq r t h n I want: aaa b c d e f fsss g h i k l qqq r t h , n ggg p t e d u qqq i o s , k (2 Replies)
Discussion started by: npatwardhan
2 Replies

9. Shell Programming and Scripting

awk - if field is empty, move line to new file

I have a script with this statement: /usr/xpg4/bin/awk -F"" 'NR==FNR{s=$2;next}{printf "%s\"%s\"\n", $0, s}' LOOKUP.TXT finallistnew.txt >test.txt I want to include logic or an additional step that says if there is no data in field 3, move the whole line out of test.txt into an additional... (9 Replies)
Discussion started by: scriptr2be
9 Replies

10. Shell Programming and Scripting

move filenames with to another filename without the last character ~

Hi, I need to move filenames with the following format below into filenames without the ~ like sample below. I hope you can help me create a simple unix script that will do this. Thanks in advance! move filename from: AIRS20081225-235641.BSP~ AIRS20081225-235648.BSP~ AIRS20081225-235640.BSP~... (3 Replies)
Discussion started by: ayhanne
3 Replies
Login or Register to Ask a Question