Inconsistent column printing


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Inconsistent column printing
# 1  
Old 04-30-2013
Inconsistent column printing

Hi,

I have a file that has inconsistently numbered columns.

Like row1 has 23 columns, and row 2 has 34 columns etc.

I would like to re-order the first 8 columns as required and from the 9th column till the end, I would like to print it as it is.

I tried to read the re-ordered 8 columns into a variable called v and then tried to print from 9th column till the end using a for loop. But, this just prints the 9th column and not the entire columns till the last.

The field separator is space.

Code:
awk '{(v=$1"\t"$4"\t"$5"\t"$2"\t"$3"\t"$6"\t"$7"\t"$8);{for(i=9;i<=NF;++i)print v"\t"$i}}' input

# 2  
Old 04-30-2013
Quote:
Originally Posted by jacobs.smith
Hi,

I have a file that has inconsistently numbered columns.

Like row1 has 23 columns, and row 2 has 34 columns etc.

I would like to re-order the first 8 columns as required and from the 9th column till the end, I would like to print it as it is.

I tried to read the re-ordered 8 columns into a variable called v and then tried to print from 9th column till the end using a for loop. But, this just prints the 9th column and not the entire columns till the last.

The field separator is space.

Code:
awk '{(v=$1"\t"$4"\t"$5"\t"$2"\t"$3"\t"$6"\t"$7"\t"$8);{for(i=9;i<=NF;++i)print v"\t"$i}}' input

Please show us some sample input, the output you're getting from your script, and the output you want to get.
# 3  
Old 04-30-2013
Quote:
Originally Posted by jacobs.smith
Hi,

I have a file that has inconsistently numbered columns.

Like row1 has 23 columns, and row 2 has 34 columns etc.

I would like to re-order the first 8 columns as required and from the 9th column till the end, I would like to print it as it is.

I tried to read the re-ordered 8 columns into a variable called v and then tried to print from 9th column till the end using a for loop. But, this just prints the 9th column and not the entire columns till the last.

The field separator is space.

Code:
awk '{(v=$1"\t"$4"\t"$5"\t"$2"\t"$3"\t"$6"\t"$7"\t"$8);{for(i=9;i<=NF;++i)print v"\t"$i}}' input


You are printing "v" inside the for loop. Which means printing

Code:
col1 .. col8 col9
col1 .. col8 col10
col1 .. col8 col11
..
..

And repeat the same for next row.
Is that you really want?

May be you need this?
Code:
awk '{v=$1"\t"$4"\t"$5"\t"$2"\t"$3"\t"$6"\t"$7"\t"$8; printf v;{for(i=9;i<=NF;++i)printf "\t"$i}print x}' input

This User Gave Thanks to clx For This Post:
# 4  
Old 04-30-2013
Maybe this is better readable?
Code:
awk 'function m(arg) {printf sep"%s",$arg; sep="\t"}
{m(1);m(4);m(5);m(2);m(3);m(6);m(7);m(8); for(i=9;i<=NF;i++) m(i); print sep=""}
' input

Or this one?
Code:
awk 'BEGIN {OFS=ORS="\t"}
{print $1,$4,$5,$2,$3,$6,$7,$8; for(i=9;i<=NF;i++) print $i; printf "\n"}
' input


Last edited by MadeInGermany; 04-30-2013 at 05:53 PM..
This User Gave Thanks to MadeInGermany For This Post:
# 5  
Old 04-30-2013
Code:
$ awk '{X=$4; Y=$5; $4=$2; $5=$3; $2=X; $3=Y}1' OFS="\t" file

This User Gave Thanks to RudiC For This Post:
# 6  
Old 05-01-2013
Quote:
Originally Posted by clx
You are printing "v" inside the for loop. Which means printing

Code:
col1 .. col8 col9
col1 .. col8 col10
col1 .. col8 col11
..
..

And repeat the same for next row.
Is that you really want?

May be you need this?
Code:
awk '{v=$1"\t"$4"\t"$5"\t"$2"\t"$3"\t"$6"\t"$7"\t"$8; printf v;{for(i=9;i<=NF;++i)printf "\t"$i}print x}' input

Thanks for your time clx.

It does what I needed. But, from col1 to col8 I want the field separator to be tab. And from col9 till the end, I would just like to print as it is in the input.

---------- Post updated at 08:31 AM ---------- Previous update was at 08:29 AM ----------

Quote:
Originally Posted by RudiC
Code:
$ awk '{X=$4; Y=$5; $4=$2; $5=$3; $2=X; $3=Y}1' OFS="\t" file

Thanks Rudic, I tried taking of the OFS, but it makes all columns to be space separated.

Is there a way to print col1 to col8 in tabs and then from col9 till the end as it is from the input?

Thanks
# 7  
Old 05-01-2013
Code:
while read f1 f2 f3 f4 f5 f6 f7 f8 rest
do
 printf '%s\t' "$f1" "$f4" "$f5" "$f2" "$f3" "$f6" "$f7" "$f8"
 printf '%s\n' "$rest" 
done < input

This User Gave Thanks to MadeInGermany 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

Inconsistent behavior when sorting by column

I'm trying to sort the list below, so that multiple instances of the form pass=i_d=j are contiguous. Curiously, the standard sort command to do this works some of the time (it produced the output below) but not all of the time. the command I used was cat fileName | sort -k1.26,1.26n... (3 Replies)
Discussion started by: LeoKSimon
3 Replies

2. UNIX for Dummies Questions & Answers

Printing out lines that have the same value in the first column but different value in the second

Hi, I have a text file that looks like the following: ILMN_1343291 6 74341083 74341772 ILMN_1343291 6 74341195 74341099 ILMN_1343295 12 6387581 6387650 ILMN_1651209 1 1657001 1657050 ILMN_1651209 5 83524260 83524309 I... (1 Reply)
Discussion started by: evelibertine
1 Replies

3. Shell Programming and Scripting

printing certain elelment of a column

"File1.txt" CHR SNP BP ANNOT 8 rs1878374 127974042 MYC(-843.5kb)|FAM84B(+334.4kb) 2 rs2042077 16883103 VSNL1(-702.2kb)|SMC6(-825.5kb)|RAD51AP2(-672.4kb)|MYCN(+878.5kb)|MSGN1(-978.2kb)|GEN1(-915.6kb)|FAM49A(+172.5kb) 12 rs10431347 3023955... (4 Replies)
Discussion started by: johnkim0806
4 Replies

4. Shell Programming and Scripting

Column printing in awk

Experts, i have a following file containing data in following manner. 1 2480434.4 885618.6 0.00 1948.00 40.00 1952.00 ... (6 Replies)
Discussion started by: Amit.saini333
6 Replies

5. Shell Programming and Scripting

Printing second column of several files into one

HI All, I have exactly 100 text files with extension .txt. The files contain numbers like this: 1.txt 0.4599994 65914 0.40706193 190743 0.39977244 185019 0.39831382 74906 0.3915928 122428 0.38844505 39999 0.38820446 72691 0.38787442 176430 0.38670844 28791 0.38597047 91091... (2 Replies)
Discussion started by: shoaibjameel123
2 Replies

6. UNIX for Dummies Questions & Answers

Printing a particular column using SED

Hi, i want to display only the particular column using SED command. For example, ps -ef|grep ash |sed -n '1p'|cut -d ' ' -f2   this gives 29067 ps -ef|grep ash |sed -n '1p'|awk '{print $2}'    this also gives the same  in the same way i need the solution using sed. Please... (4 Replies)
Discussion started by: pandeesh
4 Replies

7. Solaris

Printing a particular column[autosys]

Dear All, I'm using autosys in my production system. My concern is as follows: autosys -j <some_job_nm> Output: Job Name Last Start Last End ST Run Pri/Xit ... (1 Reply)
Discussion started by: saps19
1 Replies

8. UNIX for Dummies Questions & Answers

creating a file using the fist column and printing second column

Hello all. I have a problem that I need help solving. I would like to convert the following file: human pool1_12 10e-02 45 67 human pool1_1899 10e-01 45 29 human pool1_1829 10e-01 43 26 horse pool1_343 10e-20 65 191 horse pool1_454 10e-09 44 43... (5 Replies)
Discussion started by: viralnerd
5 Replies

9. Shell Programming and Scripting

regarding about printing row to column

Hello, I got data like that, =111 A= alpha B= 1 C= qq D= 45 F= ss G= 334 =1234 A= B= 2w C= D= 443 F= G= =3434 A= B= e3e (5 Replies)
Discussion started by: davidkhan
5 Replies

10. UNIX for Dummies Questions & Answers

Printing highest value from one column

Hi, I have a file that looks like this: s6 98 s6 91 s6 56 s5 32 s5 10 s5 4 So what I want to do is print only the highest value for each value in the column: So the file will look like this: s6 98 s5 32 Thanks (4 Replies)
Discussion started by: phil_heath
4 Replies
Login or Register to Ask a Question