Rearranging fields from a pipe


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Rearranging fields from a pipe
# 1  
Old 04-04-2005
Rearranging fields from a pipe

I have a large file that I am pulling only certain fields out of but my output I would like to rearrange the field order via a pipe. I have been looking through the site and man pages and have come to a loss.

I am running on HP

cut -c33-38,44-46,62-65,91-98 <file> | grep -e <value> > <outfile>

I would like to have field 4 moved to be the first field.

I tried something like using awk but it is not intuitive

cut -c33-38,44-46,62-65,91-98 <file> | grep -e <value> | awk '(printf("-%2s-%3s-%3s-%2s\n", $4,$1,$2,$3))' > <outfile>


thanks
# 2  
Old 04-04-2005
Some corrections
Code:
cut -c33-38,44-46,62-65,91-98 <file> | grep -e <value> | 
        awk ' {
                  printf("-%8s-%6s-%3s-%4s\n", $4,$1,$2,$3)
               } ' > <outfile>

Assuming your cut options got the right fields
# 3  
Old 04-04-2005
Perhaps just use awk like this...
Code:
awk '/value/ {print substr($0,91,8) substr($0,33,6) etc}' infile > outfile

# 4  
Old 06-16-2005
I was having the same problem and ended up using the paste command to rearrange:

cut -d , -f 1 some.csv > 1.csv
cut -d , -f 10 some.csv > 10.csv
paste -d ',' 10.csv 1.csv > combined.csv
rm 1.csv
rm 10.csv

1 and 10 are the column(field) numbers(position)
# 5  
Old 06-16-2005
Code:
cut ..... <file> | grep ... | while read field_1 field_2 field_3 field_4 ; do
     print - "$field_4 $field_1 $field_2 $field_3"
done > <outfile>

Of course awk could do exactly the same but using a lot more system resources in the process hence justifying the procurement of new hardware more early and hence is the preferable solution.

Always strife to use the wait()-function to its full potential. ;-))

Btw.: you haven't asked, but I suppose your data are in tabular form and you are 'cut'-ting the field boundaries. There is an easier way to do that if the field entries contain no blanks:

Code:
sed 's/<space><space>*/<space>/g' <file> | cut -d'<space>' -f<1-4>

bakunin
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Rearranging the column

I have a text file like this, I would like to rearrange the first column (Name) according to the third column(percentage)in descending order. I mean methionine with the highest percentage should be the first one to appear under the name column. But I also want to exclude the headers from this... (2 Replies)
Discussion started by: cathum
2 Replies

2. Shell Programming and Scripting

Rearranging Data Set

Hello everybody, I've got the following problem: The data set I have is an ASCII file containing a header over 4 lines and the actual data comprised of dezimal numbers in a 1000x1000 grid (1000 lines and 1000 columns). Since I want to plot the data in GMT I need to convert it into the... (3 Replies)
Discussion started by: Evilknievel
3 Replies

3. Shell Programming and Scripting

Help with rearranging file with script

Hi Guys I normally do thins with a Windows program but I am trying to rearrange a filename based on delimiters in Ubuntu. Example v017 __ Detective Academy Q #133 Murder in the Village Of Suspension Bridges &&& Part 9.cbz = Detective Academy Q v017 #133 Murder in the Village Of Suspension... (9 Replies)
Discussion started by: itschrisonline
9 Replies

4. Shell Programming and Scripting

Counting and rearranging the rows

Hi, I have a file that I re-arranged using awk and unix commands to produce a file that looks like this: JOE JOE JOE JOE JOE BOB BOB HI HI HI I want to count how many of the same rows there are and print it on the second column while only maintaining the original name once. The... (5 Replies)
Discussion started by: phil_heath
5 Replies

5. Shell Programming and Scripting

Rearranging into new columns (awk?)

Hi experts, I've used several solutions from this forum to delete nonsense and rearrange data in the project file I'm working on. I'm hoping you guys can give me some tips on further rearranging the data (I've seen a few solutions by searching, but one specific item has me stumped, which is only... (5 Replies)
Discussion started by: coryvp
5 Replies

6. Shell Programming and Scripting

Rearranging

Hello, I spent all day trying to write a script and cannot find the solution :( I have plenty files looking like this: several hundred lines precede the following interesting Bla xxx: Blub = -7537.37687 Blub = -100.644746 Blub = -3247.61954 . . . Blub = 1324.82567 Blub =... (2 Replies)
Discussion started by: tempestas
2 Replies

7. Shell Programming and Scripting

Large pipe delimited file that I need to add CR/LF every n fields

I have a large flat file with variable length fields that are pipe delimited. The file has no new line or CR/LF characters to indicate a new record. I need to parse the file and after some number of fields, I need to insert a CR/LF to start the next record. Input file ... (2 Replies)
Discussion started by: clintrpeterson
2 Replies

8. UNIX for Dummies Questions & Answers

Rearranging whole columns

Hello all, I have a text file that is arranged: name 3 7 2 9 5 jim a d e g k max d g u x g rob f w v k o This is just an example as my real file has >1000 individuals and >64,000 columns. I need to rearrange the file so that the columns appear in numerical order so that name... (3 Replies)
Discussion started by: doobedoo
3 Replies

9. Shell Programming and Scripting

Rearranging columns

Hi, I have an input file as follows : input.txt abcdTXXqwe axdfSYYrew dasgTXXqwt gtfsTYYwer gadfSXXerw gwerSYYTXX Now I have to get four output files. output1.txt should have the first four cloumns, Where the rows containing 5th column as T and 6th-7th columns as XX output2.txt... (5 Replies)
Discussion started by: sudhamacs
5 Replies

10. Shell Programming and Scripting

Trimming fields for comma or pipe seperated file

I have file like this FileA: abc , "helloworld" , america def,asia, japan ghi, africa, ipl Output Needed: abc,"helloworld",america def,asia,japan ghi,africa,ipl I would like to implement using awk. I want to trim each field for its leading and trailing spaces. (7 Replies)
Discussion started by: pinnacle
7 Replies
Login or Register to Ask a Question