line to column using awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting line to column using awk
# 1  
Old 01-16-2008
Question line to column using awk

hi,

i'm a newbie and this is my first post here. 'hope all of you fellow members are doing fine. so here is my first thread to ask for help on how to use awk language to do this task.

i have a file to process and after a series of other awk commands and shell scripts i managed to convert the output into something like this...

string1
string2
string3
string4
string5
string(n)

and by the awk command...

awk 'BEGIN {FS="\n";RS="";ORS=""} { x=1; while (x<NF) {print $x"\t"; x++} print $NF "\n"}'

i get the output...

string1 string2 string3 string4 string5 string6 string7 ....... string(n) - all in one line separated by tab

now, my last task is to separate this line into 3 column each line like...

string1 string2 string3
string4 string5 string6
string7 string8 string(n)

my guess is that i can pipe another awk command to my last awk above or i can just disregard that awk command that converts all string in one line and go directly to the 3 column format but i haven't got any clue on how to do as well. can anyone help me code this in awk?

thanks in advance! Smilie

Last edited by genix2008; 01-16-2008 at 11:39 PM..
# 2  
Old 01-17-2008
Try:
Code:
awk 'function f (ind,x){for (o=1;o<=ind;o++)printf("%s ",x[o]);printf("\n")} 
     {i++;a[i]=$0;if ( i == 3 ){f(i,a);i=0}}END{if ( i < 3 && i > 0 )f(i,a) }' file

# 3  
Old 01-17-2008
Code:
awk 'NR%3!=0{
 printf "%s ", $0
}
NR%3==0{
 printf "%s\n", $0
}' file

# 4  
Old 01-17-2008
# cat file
string1
string2
string3
string4
string5
string6
string7
string8
string(n)

Code:
#  paste - - - < file1
string1 string2 string3
string4 string5 string6
string7 string8 string(n)

# 5  
Old 01-17-2008
Quote:
Originally Posted by ghostdog74
Code:
awk 'NR%3!=0{
 printf "%s ", $0
}
NR%3==0{
 printf "%s\n", $0
}' file

Nice , a slight modification to your code:
Code:
awk 'NR%3!=0{printf "%s ", $0;next}1' file

# 6  
Old 01-17-2008
Or even:

Code:
awk 'ORS=NR%3?" ":"\n"' filename


On Solaris use nawk or /usr/xpg4/bin/awk.
# 7  
Old 01-17-2008
Code:
nawk 'ORS=(FNR%3)?FS:RS' myFile

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Add column to end of each line with ´awk´

Hi, I have data with approximately 300 columns. I want to add a column to the end of each column with the value "1". Is there a way that I can do this is ´awk´ without having to specify each individual column. For instance, my data looks like: pvb 1 2 3 4 5 ....... 300 fdh 3 4 5 2 4 ......... (4 Replies)
Discussion started by: owwow14
4 Replies

2. UNIX for Dummies Questions & Answers

awk help: how to pull phrase and one column from line above?

Hi everyone, Here's my awk statement so far: awk '/TOTAL TYPE:/{print x;print};{x=$0}' file1 >file2 'file1' has too much proprietary data in it to include here, so let's go with the output from code above. It looks like this: 123456 JAMES T KIRK D ... (2 Replies)
Discussion started by: Scottie1954
2 Replies

3. Shell Programming and Scripting

KSH or Awk first and last line based on column 2

Example: 10.160.101.160,0707073711,22.203.203.200 10.160.101.160,0707075132,22.203.210.249 10.160.101.160,0707085436,22.203.210.249 10.160.101.160,0707091712,22.203.221.176 10.160.101.160,0707091811,22.203.221.176 10.160.101.160,0707091845,22.203.221.176... (1 Reply)
Discussion started by: BrownBob
1 Replies

4. Shell Programming and Scripting

awk , conditional involving line and column

Dear All, I indeed your help for managing resarch data file. for example I have, data1.txt : type of atoms z vz Si 34 54 O 20 56 H 14 13 Si 40 17 O ... (11 Replies)
Discussion started by: ariesto
11 Replies

5. Shell Programming and Scripting

Awk next line as column

Hi, This forum rocks. I think this might be an easy thing, but since I am new to awk, please help me. input: x y z 1 a b c 2 d e f 3 g h i 7 output: x y z 1 a b c 2 d e f 3 (8 Replies)
Discussion started by: jacobs.smith
8 Replies

6. Shell Programming and Scripting

Counting rows line by line from a specific column using Awk

Dear UNIX community, I would like to to count characters from a specific row and have them displayed line-by-line. I have a file called testAwk2.csv which contain the following data: rabbit penguin goat giraffe emu ostrich I would like to count in the middle row individually... (4 Replies)
Discussion started by: vnayak
4 Replies

7. Shell Programming and Scripting

awk : Remove column1 and last column in a line

Hi All, How to remove col1 and last column in a line. Please suggest some awk stuffs. Input col1 col2 col3 col4 col1 col2 col3 col4 col5 col1 col2 col3 col4 col1 col2 col3 Output Processing col2 col3 ... Processing col2 col3 col4 ... Processing col2 col3 ... Processing... (5 Replies)
Discussion started by: k_manimuthu
5 Replies

8. Shell Programming and Scripting

awk search column, print line

Hello. I've been banging my head against walls trying to search a comma delimited file, using awk. I'm trying to search a "column" for a specific parameter, if it matches, then I'd like to print the whole line. I've read in multiple texts: awk -F, '{ if ($4 == "string") print $0 }'... (2 Replies)
Discussion started by: Matthias03
2 Replies

9. Shell Programming and Scripting

awk convert from line to column

i have an output like this : 012008 25760883 022008 12273095 032007 10103 032008 10115642 042007 20952798 but i would like to have it like this 012008,25760883 022008,12273095 032007,10103 032008,10115642 042007,20952798 (4 Replies)
Discussion started by: jarmouda
4 Replies

10. Shell Programming and Scripting

awk to select a column from particular line number

The awk command awk -F: '{print $1}' test1 gives the first columns of all the lines in file ,is there some command to get a particular column from particular line . Any help is appreciated. thanks arif (4 Replies)
Discussion started by: mab_arif16
4 Replies
Login or Register to Ask a Question

Featured Tech Videos