AWK question 
Author Message
 AWK question

I want to print out the first 3 field of a line separated by the FS
and the rest of the line concatenated together...how do I do it?

ie:

awk " { print $1,$2,$3, *the rest of the line*} " input.file

thanks



Wed, 03 Nov 2004 04:15:11 GMT  
 AWK question

Quote:
>I want to print out the first 3 field of a line separated by the FS
>and the rest of the line concatenated together...how do I do it?

>ie:

>awk " { print $1,$2,$3, *the rest of the line*} " input.file

Find out where the 4th field starts and print from there

{
match($0,/[ \t]*[^ \t]*[ \t]*[^ \t]*[ \t]*[^ \t]*[ \t]*/)
print $1,$2,$3,substr($0,1+RLENGTH,9999)

Quote:
}

>thanks

byefrom

--

                # celebration sig. for her W/End debut.Can u crack it?
mADonnAmaDOnnAmAdONnaMADoNNaMadOnNAmADOnnaMadonnamADoNNamaDOnNamadONnA
madoNnamadoNnaMAdonNAmadoNnAMadOnnaMADOnnAmAdoNNaMadonNaMadoNnAMadonNA
maDonnAMadOnnaMadonNaMadONnamaDONnAMaDoNnAMAdoNNAmaDoNnamAdOnnAmAdonna
MadONNaMadoNnAmAdoNNAMadONNAmAdoNNamadoNnAmAdonnAmAdOnnamaDoNnaMAdonna
madoNnAmaDOnnAmAdONnAMAdoNNamAdonnamAdOnnamadonnamadonnamadonnamadonna



Wed, 03 Nov 2004 05:53:52 GMT  
 AWK question

Quote:
> I want to print out the first 3 field of a line separated by the
> FS and the rest of the line concatenated together...how do I do
> it?

> ie:

> awk " { print $1,$2,$3, *the rest of the line*} " input.file

> thanks

Perhaps, you mean something like this, looking at Ie.: /etc/passwd:

BEGIN{FS=":"}{for (i=1;i<=3;i++) printf "%s",$i":";
for (i=4;i<=NF;i++) printf "%s",$i;print ""}

Good luck

Michael Heiming
--
Remove the +SIGNS case mail bounces.



Wed, 03 Nov 2004 06:27:39 GMT  
 AWK question

% I want to print out the first 3 field of a line separated by the FS
% and the rest of the line concatenated together...how do I do it?

It's not clear what you mean by this, and your example doesn't really
make it more clear. Could you post an example input and output?

I'll guess that you want to treat a record as if it had four
fields, the first three separted by FS, and the last one consisting
of the rest of the line. One way of doing this is to use match
to match the first three fields. Exactly how you do this depends
on the value of FS, but here are some examples:

For the default:
 FS = " "
 FSASRE = "[ \t]+"
 NOTFS = "[^ \t]*"
 firstthree = "^[ \t]*" NOTFS FSASRE NOTFS FSASRE NOTFS FSASRE

 # this is the same for all the examples
 if (match($0, firstthree))
   four = substr($0, RLENGTH+1)
 else
   four = ""

 # do whatever it was you need to do
 print $3, $2, $1, four

For a single-character FS:
 FS = "."
 FSASRE = "[.]"
 NOTFS = "[^.]*"
 firstthree = "^" NOTFS FSASRE NOTFS FSASRE NOTFS FSASRE

For a multi-character FS:

 FS = ",+"
 NOTFS = "[^,]*"
 firstthree = "^" NOTFS FS NOTFS FS NOTFS FS

If that's not helpful, you might want to post more details about your
problem.
--

Patrick TJ McPhee
East York  Canada



Thu, 04 Nov 2004 02:28:05 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Simple awk question

2. A Newbie AWK question

3. awk question

4. awk question about matching a pattern

5. newbie awk question

6. awk questions !!!

7. Simple awk question

8. Easy Awk Question

9. Awk question.

10. awk Question

11. awk question

12. awk question

 

 
Powered by phpBB® Forum Software