Limit output in awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Limit output in awk
# 1  
Old 03-11-2010
Limit output in awk

Hi All,

I want to delimit the output using awk.
For eg::
Code:
a,b,c,d,e,f
abc,def,ghi

should change to
Code:
a|b|c|d|e|f
abc|def|ghi

Or to put in another way..

OFS will work in the following code if I seperate the values with comma (eg. $1,$2,$3,$4,$5,$6 ). But since the no. of fields are variable, I might have to use some more lengthy steps.
Is there some way to make OFS work if I use following code (or little variation)..
Code:
str="a,b,c,d,e,f"
echo "$str" | awk -F"," 'BEGIN{OFS="|"}{print $0}'


Thanks..
# 2  
Old 03-11-2010
Code:
awk -F, '{$1=$1}1' OFS="|" file

or:

Code:
sed 's/,/|/g' file

or:

Code:
tr ',' '|' < file

# 3  
Old 03-11-2010
Thanks Franklin,

Can you please explain the awk one..

thanks..
# 4  
Old 03-11-2010
Quote:
Originally Posted by PRKS
Thanks Franklin,

Can you please explain the awk one..

thanks..
Sure:
Code:
awk -F, '{$1=$1}1' OFS="|" file

Explanation:

Set fieldseparator:
Code:
-F,

Rearrange the line with the new fieldseparator:
Code:
$1=$1

Print the line, similar to {print $0}:
Code:
1

Set new fieldseparator:

Quote:
OFS="|"
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Limit the number of characters in bash output

Hi, I need some help with this: I'm making a script which does a couple of things with image files. The script is supposed to echo the number of each image it is processing like this: Processing image1.jpgThe problem is with images with very long filenames, so I want to know how to limit the... (5 Replies)
Discussion started by: Shadow_Reaper
5 Replies

2. UNIX for Dummies Questions & Answers

character limit via awk

I'm using a command that outputs the total size of the files that I've specified. I'd like to introduce a character limit that appends an ellipsis to the lines that go beyond the specified amount. du -chs {query} | sed 's!.*/!!' | awk '{print substr($0,0,25)""}' That's what I have so far.... (4 Replies)
Discussion started by: Light_
4 Replies

3. UNIX for Dummies Questions & Answers

Limit the number of characters in a bash output

I have a script that outputs the weather on two lines. If possibly I would like to set a character limit on them Currently it outputs something like but I would like to limit the lines so appends an ellipsis if nescessary: This is the script #! /bin/bash curl -s --connect-timeout... (2 Replies)
Discussion started by: Light_
2 Replies

4. Shell Programming and Scripting

AWK Memory Limit ?

Is there an input file memory limit for awk? I have a 38Mb text file that I am trying to print out certatin lines and add a string to the end of that line. When I excute the script on the 38Mb file the string I am adding is put on a new line. If I do the same with a smaller file the... (3 Replies)
Discussion started by: cold_Que
3 Replies

5. Shell Programming and Scripting

Problem with awk awk: program limit exceeded: sprintf buffer size=1020

Hi I have many problems with a script. I have a script that formats a text file but always prints the same error when i try to execute it The code is that: { if (NF==17){ print $0 }else{ fields=NF; all=$0; while... (2 Replies)
Discussion started by: fate
2 Replies

6. Shell Programming and Scripting

How to group the output w/ limit

Hi All, Second time to post on this group :) I'm pulling my hair now 'coz I'm so dumb to produce this requirement. Requirement: I want to run a utility by limiting the no. inside my process (mov##) to be able to use in multi streaming. Here is my script: --Input: "user_list.txt"... (0 Replies)
Discussion started by: alvingo
0 Replies

7. Shell Programming and Scripting

AWK limit for (length) function?

I am trying to use the following code: awk '{s=$0;if(length(s) < 750){getline; s=s " " $0}printf("%s\n",s)}' filename but an error shows that 'awk' is too long. Is there a limit to the awk length function? and what could be an alternate solution for long fixed width records? The code... (3 Replies)
Discussion started by: CKT_newbie88
3 Replies

8. Shell Programming and Scripting

awk file size limit

My code is awk '{ out=split(FILENAME,a,"/") sub(/\./,"_",a) sub(/\-/,"_",a) NEWSTRING="main_"a"_"a"(" # The word we want to insert gsub(/main\(/,NEWSTRING); # the word to be replaced print "Main becomes ",NEWSTRING")","in file ",FILENAME >> "/home/ds2/test/NEW2.txt" ... (4 Replies)
Discussion started by: senior_ahmed
4 Replies

9. Shell Programming and Scripting

How to limit output

hello, i'm trying to figure out a way to limit the output from an SQL query that is counting the number of occurances of a value in a field and the problem is when i run this query against a huge file with many unique values the output is pretty huge. Is there a way i can specifically LIMIT the... (2 Replies)
Discussion started by: bobk544
2 Replies

10. Shell Programming and Scripting

Is there a way to limit DIFF output

Hello is there a way to limit the number of lines output by the DIFF command? I tried -C 200 ect and -c but it continues to print out the whole huge file. Reason needed is i'm trying to do alot of DIFFs on a long list of files and would like to only get back an indicator which files are... (2 Replies)
Discussion started by: bobk544
2 Replies
Login or Register to Ask a Question