awk to print all row to one line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk to print all row to one line
# 8  
Old 10-15-2010
Code:
tr -d ',\n' < infile

# 9  
Old 10-15-2010
Thank you ver much Manumuth !! Above one worked fine !!
# 10  
Old 10-16-2010
... I am quite a noob in awk but aware of its power, i wanted to learn more about it.

Could someone explain me the '$1=$1' ORS= part of the code ?
i know that ORS stand for Output Record Separator, but i don't get the equal nothing, nor the $1=$1 (assign first field to first field?)

Quote:
Originally Posted by Scrutinizer
Code:
awk -F, '$1=$1' ORS= infile

And in that one, i just don't get the 1 ...

Code:
awk -vORS=' ' 1 FILE

Thanks in advance for your time & explaination
# 11  
Old 10-16-2010
Quote:
Originally Posted by ctsgnb
... I am quite a noob in awk but aware of its power, i wanted to learn more about it.

Could someone explain me the '$1=$1' ORS= part of the code ?
i know that ORS stand for Output Record Separator, but i don't get the equal nothing, nor the $1=$1 (assign first field to first field?)
The record ($0) gets rebuilt once you modify a field ($1, $2, etc). Assigning $1 to $1 does not change the field itself, since it gets assigned its own value, but it causes the record layout to be rebuilt using OFS instead of FS so in this case the comma separator gets changed to a space.

The ORS= means the output record becomes "" so everything gets printed on the same line.
# 12  
Old 10-16-2010
Quote:
Originally Posted by Scrutinizer
The record ($0) gets rebuilt once you modify a field ($1, $2, etc). Assigning $1 to $1 does not change the field itself, since it gets assigned its own value, but it causes the record layout to be rebuilt using OFS instead of FS so in this case the comma separator gets changed to a space.

The ORS= means the output record becomes "" so everything gets printed on the same line.
Ok thx
# 13  
Old 10-16-2010
Quote:
Originally Posted by ctsgnb
So the following code would not be sufficient to change the , into a space ?
Code:
awk -F, ORS=' ' infile

I mean the record layout wouldn't be rebuilt in spite of the fact we specified the ORS?
Correct.
Code:
$ printf '1,2,3\n5,6,7\n'
1,2,3
5,6,7

Code:
$ printf '1,2,3\n5,6,7\n' | awk -F, 1 ORS=' '
1,2,3 5,6,7

Code:
$ printf '1,2,3\n5,6,7\n' | awk -F, '$1=$1' ORS=' '
1 2 3 5 6 7

# 14  
Old 10-16-2010
i still don't get that 1 in the statement

Code:
awk -F, 1 ORS=' '

The 1 refer to the first line ? or to the first record ? or something else ?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Print all lines in a row except the last one with awk

Hi again, is it possible to do the following using awk? input file is 4.465E+17 5.423E+16 1.218E+17 2.600E+16 9.135E+15 1.238E+14 ... 6.238E+14 desired output 4.465E+17 & 5.423E+16 & 1.218E+17 & 2.600E+16 & 9.135E+15 & 1.238E+14 & ... & (3 Replies)
Discussion started by: f_o_555
3 Replies

2. Shell Programming and Scripting

If the line is not the same as the next line then print the row

a b c d e f g h a b c d f f g h it should print 5 (8 Replies)
Discussion started by: yanglei_fage
8 Replies

3. Shell Programming and Scripting

Print list to row using awk

Hello, can somebody help me on this please. I have a list of numbers and I want to print them in one line seprated by a comma except the last one using awk 34 12 56 76 88 output 34,12,56,76,88 Thanks Sara (15 Replies)
Discussion started by: Sara_84
15 Replies

4. UNIX for Dummies Questions & Answers

awk to print first row with forth column and last row with fifth column in each file

file with this content awk 'NR==1 {print $4} && NR==2 {print $5}' file The error is shown with syntax error; what can be done (4 Replies)
Discussion started by: cdfd123
4 Replies

5. Shell Programming and Scripting

Print unique names in each row of a specific column using awk

Is it possible to remove redundant names in the 4th column? input cqWE 100 200 singapore;singapore AZO 300 400 brazil;america;germany;ireland;germany .... .... output cqWE 100 200 singapore AZO 300 400 brazil;america;germany;ireland (4 Replies)
Discussion started by: quincyjones
4 Replies

6. Shell Programming and Scripting

print the whole row in awk based on matched pattern

Hi, I need some help on how to print the whole data for unmatched pattern. i have 2 different files that need to be checked and print out the unmatched patterns into a new file. My sample data as follows:- File1.txt Id Num Activity Class Type 309 1.1 ... (5 Replies)
Discussion started by: redse171
5 Replies

7. Shell Programming and Scripting

AWK Script - Print a column - within a Row Range

Hi, Please read the whole thread. I have been working on this script below. It works fine, feel free to copy and test with the INPUT File below as well. example: PACKET DATA PROTOCOL CONTEXT DATA APNID PDPADD EQOSID VPAA PDPCH PDPTY PDPID 10 ... (6 Replies)
Discussion started by: panapty
6 Replies

8. Shell Programming and Scripting

awk print specific columns one row at a time

Hello, I have the following piece of code: roleName =`cat $inputFile | awk -F';' '{ print $1 }'` roleDescription =`cat $inputFile | awk -F';' '{ print $2 }'` roleAuthProfile =`cat $inputFile | awk -F';' '{ print $3 }'` mappedUserID (5 Replies)
Discussion started by: pr0tocoldan
5 Replies

9. Shell Programming and Scripting

Awk Line/Row Position

Hi guys. I'd just like to know if its possible to change the actual line/row position in awk while its busy processing a file. In other words, is it possible to jump from line 10, back up to line 5 and continue processing line-by-line from then onwards? or is the way around this to add all lines... (3 Replies)
Discussion started by: going_grey
3 Replies

10. Shell Programming and Scripting

shell script(Preferably awk or sed) to print selected number of columns from each row

Hi Experts, The question may look very silly by seeing the title, but please have a look at it clearly. I have a text file where the first 5 columns in each row were supposed to be attributes of a sample(like sample name, number, status etc) and the next 25 columns are parameters on which... (3 Replies)
Discussion started by: ks_reddy
3 Replies
Login or Register to Ask a Question