AwK printf question


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers AwK printf question
# 1  
Old 09-14-2006
Data AwK printf question

Hi,
Does anyone know a easy way to printf $3,$4, ... all the way to the last field in the file? I will need to modify $1 and $2 and then printf modified $1 and $2 and the rest of the fields(which are not changed).
I know I can use NF as the total number of field.
Do I use a for next statement to loop through?

Thanks for your help!
# 2  
Old 09-14-2006
$1="xxxx"
$2="yyyy"
#if you want to print the whole line then do this
print $0
# 3  
Old 09-14-2006
awk

Thank you for your reply.
The things is everytime the file that passed to awk is different.
So I don't know the exact number of columns.
But I only modify $1,$2. There might be over 10 fields after that.
It's really $0 - $1 - $2.
I need to write out any fields after $1 and $2.

Any hint?
# 4  
Old 09-14-2006
use for loop to do that
# 5  
Old 12-10-2007
You don't need a for loop

I know it's been over a year, but maybe someone else will benefit from this.
If you want to print everything but a number of fields, set them to "" (null)

So, to print everything past $2, type:
awk '{ $1=""; $2=""; print $0}' foo.log

This can be shortened to:
awk '{ $1=$2=""; print $0}' foo.log

Be sure never to set $0 to null, because that will nuke your entire string.Smilie
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Printf question: getting padded zero in decimal plus floating point together.

Hi Experts, Quick question: I am trying to get the output with decimal and floating point but not working: echo "20.03" | awk '{printf "%03d.2f\n" , $0 }' 020.2f How to get the output as : 020.03 Thank you. (4 Replies)
Discussion started by: rveri
4 Replies

2. Shell Programming and Scripting

AWK printf command question

Hi, I am using below awk code to convert a csv file data into fixed file format. awk 'BEGIN { FS = "," fmt = "%10s%010d%10s%d%1d\n" } NR>1 { printf fmt, $1, $2, $3, $4*100, $5 }' /data/mydata.csv > /data/fixed.dat Data in mydata.csv ================... (2 Replies)
Discussion started by: kbmkris
2 Replies

3. UNIX for Dummies Questions & Answers

AWK printf command question

Hi, I am using below awk code to convert a csv file data into fixed file format. awk 'BEGIN { FS = "," fmt = "%10s%010d%10s%d%1d\n" } NR>1 { printf fmt, $1, $2, $3, $4*100, $5 }' /data/mydata.csv > /data/fixed.dat Data in mydata.csv ================... (1 Reply)
Discussion started by: kbmkris
1 Replies

4. Shell Programming and Scripting

awk with printf

Hi, I am using the following code to assign a count value to a variable. But I get nothing. Do you see anything wrong here. I am new to all this. $CTR=`remsh $m -l $MACHINES{$m} -n cat $output | grep -v sent | grep \"$input\" | sort -u | awk '{print $5}'`; Upto sort - u it's... (2 Replies)
Discussion started by: nurani
2 Replies

5. Shell Programming and Scripting

AWK printf help

Target file contains short text (never more than 1 line) and filenames. The format is, e.g.,: TEXT1 filename1 TEXT2 TEXT3 filename3dddd filename3dddd TEXT4 filename4 TEXT5 filename5dddd filename5dddd filename5 where dddd is a random 4-digit whole number. Desired output: (4 Replies)
Discussion started by: uiop44
4 Replies

6. Shell Programming and Scripting

IF and awk/printf

Hi Friends, Scripting newb here. So I'm trying to create a geektool script that uses awk and printf to output certain fields from top (namely command, cpu%, rsize, pid and time, in that order). After much trial and error, I've pretty much succeeded, with one exception. Any process whose name... (3 Replies)
Discussion started by: thom.mattson
3 Replies

7. Shell Programming and Scripting

awk and printf

echo $bbsize 1.5 echo $fillpercent .95 echo $bbsize | awk '{printf "%.2f\n",$0*$fillpercent}' 2.25 echo $bbsize | awk '{printf "%.2f\n",$0*.95}' 1.42 1.42 is what I'm expecting... echo $blocksize 4096 echo $bbsize | awk '{printf "%.2f\n",$0*$blocksize}' 2.25 echo $bbsize |... (3 Replies)
Discussion started by: xgringo
3 Replies

8. Shell Programming and Scripting

printf question

can you take input from another command and do printf? such as awk '{print $2,$1}' | sort -k1,1 -k2,2 | printf "%-10s,%15s" this does not work.. but there must be a way.. please help me.. thank you. (3 Replies)
Discussion started by: hankooknara
3 Replies

9. Programming

multiple forks and printf question

Hello *NIX gurus, I have a slight perplexing problem with multiple forks giving different results... Here is the deal. From what I undestand, a fork() call starts executing from the next instruction that follows the fork() call. That means it inherits the PC counter register value of the... (4 Replies)
Discussion started by: navigator
4 Replies

10. Programming

Question: pthread_cancel() and printf()

Hello! First of all, sorry for my English, I'm not a native English speaker. I know, that printf() function uses write() function. "man cancellation" says that write() function is a cancellation point. But when I call pthread_cancel() for my thread, which calls printf() in infinite cycle, it... (4 Replies)
Discussion started by: prankster
4 Replies
Login or Register to Ask a Question