awk - treating remaining columns as one


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk - treating remaining columns as one
# 8  
Old 11-13-2012
Quote:
Originally Posted by balajesuri
This doesn't capture all fields from $3 to NF. Please re-check.
@Bala :

What makes you say that, did you try my code ?
Please recheck the initial example given Smilie

...

Code:
# cat t1
1 2 This is a comment
what the_hell? This is another comment
1 2 This is just another comment
you are just wrong :)
# awk 'sub(".*"$3,$1RS$2RS$3)' t1
1
2
This is a comment
what
the_hell?
This is another comment
1
2
This is just another comment
you
are
just wrong :)
#

---------- Post updated at 01:12 AM ---------- Previous update was at 01:01 AM ----------

Regarding the previous requested explaination about the code :

awk '{a=$1"\n"$2;sub(".*"$3,$3);print a"\n"$0}' yourfile

awk '{start of awk statement
a=$1"\n"$2hold field $1 and $2 separated by a new line, into variable a
sub(".*"$3,$3)replace in the current line ($0) everthing before and including $3 by the current value of $3
print a"\n"$0print the variable a and the current line separated by a new line
}' yourfileend of awk statement with yourfile passed as argument

awk 'sub(".*"$3,$1RS$2RS$3)' t1

awk 'start of awk statement
sub(".*"$3,$1RS$2RS$3)In the current line, substitutes everything before and including $3, with $1 $2 and $3 separated by RS (the Record Separator, whose default value is \n )
' t1end of awk statement with file t1 passed as argument

Last edited by ctsgnb; 11-13-2012 at 08:19 PM..
# 9  
Old 11-13-2012
I am almost achieving my desired output, however I am getting something funny.

Consider

alpha 100 this is a comment
bravo 200 this is another comment

When using

Code:
awk '{print $2,sub (".*"$3,$3) $0}' filename

i am getting

100 1this is a comment
200 1this is another comment

Where is this "1" between the columns I am getting coming from?
# 10  
Old 11-13-2012
@ctsgnb: It does work for file t1 in post #8. And you might also want to take a look at this. May be the awk installed on my system is having a hangover ;-)

Code:
[root@host dir]# cat file
alpha 200 this is a comment for this record
bravo 400 this is another comment for this record
[root@host dir]#
[root@host dir]# awk 'sub(".*"$3,$1RS$2RS$3)' file
alpha
200
this record
bravo
400
this record
[root@host dir]#

Instead of "this is a comment for this record", it just prints "this record". I'm using GNU Awk 3.1.5
# 11  
Old 11-13-2012
@Bala :

Ah yup,not the same version here : it was working with GNU awk 3.1.1 Smilie

Could you try to run
Code:
awk 'sub(".*"$3,$1 RS $2 RS $3,$0)' yourfile

Just to see if it works better ?

---------- Post updated at 02:14 AM ---------- Previous update was at 02:06 AM ----------

@ Bala,

By the way, i am also curious to see what output you get by running :
Code:
awk '{a=$1"\n"$2;sub(".*"$3,$3);print a"\n"$0}' yourfile

---------- Post updated at 02:18 AM ---------- Previous update was at 02:14 AM ----------

Quote:
Originally Posted by ppucci
I am almost achieving my desired output, however I am getting something funny.

Consider

alpha 100 this is a comment
bravo 200 this is another comment

When using

Code:
awk '{print $2,sub (".*"$3,$3) $0}' filename

i am getting

100 1this is a comment
200 1this is another comment

Where is this "1" between the columns I am getting coming from?
@pucci:

Fix your code :

Code:
awk '{print $1"\n"$2;sub(".*"$3,$3);print $0}' filename

or
Code:
awk '{print $1 RS $2 RS ((sub(".*"$3,$3))?$0:z)}' filename

# 12  
Old 11-13-2012
How about:
Code:
awk '{$1=$1; sub(FS,RS); sub(FS,RS)}1' infile

# 13  
Old 11-13-2012
Quote:
Originally Posted by ctsgnb
@ Bala,

By the way, i am also curious to see what output you get by running :
Code:
awk '{a=$1"\n"$2;sub(".*"$3,$3);print a"\n"$0}' yourfile

Same. I tried on a different version too (CYGWIN, GNU Awk 4.0.0) and got the same result. Baffling!

Code:
[user@home-pc ~]$ cat file
alpha 200 this is a comment for this record
bravo 400 this is another comment for this record
[user@home-pc ~]$
[user@home-pc ~]$ awk '{a=$1"\n"$2;sub(".*"$3,$3);print a"\n"$0}' file
alpha
200
this record
bravo
400
this record
[user@home-pc ~]$

Strange that it works on GNU Awk 3.1.1! Anyway, this one by Scrutinizer is pretty creative Smilie good one mate!
Code:
awk '{$1=$1; sub(FS,RS); sub(FS,RS)}1'

# 14  
Old 11-14-2012
@cts. bala@ the difference occurs not because of awk versions but because you are using different different data samples. With bala's data sample this part of ctsgnb's code is problematic: sub(".*"$3,$3) , which matches upto the last occurence of "this", because of greedy matching..



--
@cts: nice sed/xargs
@bala: thanks Smilie
This User Gave Thanks to Scrutinizer For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Awk: duplicate column and print remaining as is

Hello there I'd like to make a copy of 2nd column and have it printed in place of column 1. Remaining columns are needed as it. test data: ProbeSet GeneSymbol X22565285 X22566285 ILMN_1050008 MYOCD 6.577 7.395 ILMN_1050014 GPRC6A 6.595 6.668 ILMN_1050017 ... (2 Replies)
Discussion started by: genome
2 Replies

2. Shell Programming and Scripting

AIX to RHEL migration - awk treating 0e[0-9]+ as 0 instead of string issue

Greetings Experts, We are migrating from AIX to RHEL Linux. I have created a script to verify and report the NULLs and SPACEs in the key columns and duplicates on key combination of "|" delimited set of big files. Following is the code that was successfully running in AIX. awk -F "|" 'BEGIN {... (5 Replies)
Discussion started by: chill3chee
5 Replies

3. Shell Programming and Scripting

awk treating variables differently in UNIX-Linux

Hi, awk seem to be acting differently in Unix and Linux when it comes to formatting. This is making it difficult to migrate scripts. for example: UNIX: echo "123" |awk '{printf ("%05s\n" ,$1)}' 00123 echo "123" |awk '{printf ("%05d\n" ,$1)}' 00123 echo "S12" |awk '{printf ("%05s\n"... (9 Replies)
Discussion started by: wanderingmind16
9 Replies

4. Shell Programming and Scripting

Comparison treating strings as zero integers

I'm trying to write a bash script to perform basic arithmetic operations but I want to run a comparison on the arguments first to check that they're a number greater than zero. I want an error to pop up if the arguments args aren't >= 0 so I have: if ! ]; then echo "bad number: $1" fi ... (14 Replies)
Discussion started by: TierAngst
14 Replies

5. Shell Programming and Scripting

treating multiple delimiters[solved]

Hi, I need to display the last column value in the below o/p. sam2 PS 03/10/11 0 441 Unable to get o/p with this awk code awk -F"+" '{ print $4 }' pwdchk.txt I need to display 441(in this eg.) and also accept it as a variable to treat it with if condition and take a decision.... (1 Reply)
Discussion started by: sam_bd
1 Replies

6. Shell Programming and Scripting

Treating string as date ?

Is there a way to treat a string as date and compare it to the current date? lets assum inpu lik $ cat myfile Name Last login ************************** Sara 2/13/2012 kalpeer 2/15/2012 ygemici 2/14/2012 we want to display the name who logged in during the last #... (4 Replies)
Discussion started by: Sara_84
4 Replies

7. Shell Programming and Scripting

Treating Strings with spaces

I have a file list.txt which has a list of file names with spaces between the file names like /emptydir/file 1 how do i browse through the list.txt displaying the filenames. Almost all the file names in list.txt have space between them.This file list.txt is formed by using the find statement to... (5 Replies)
Discussion started by: kinny
5 Replies

8. Shell Programming and Scripting

TRAP treating

Hi, I'm looking for a script that receives the traps from a windows machine and treate them. For exemple just write a line in a file on UNIX server. Can you help me ? Thank you. (2 Replies)
Discussion started by: big123456
2 Replies

9. Shell Programming and Scripting

treating special chars

Hi, I need some advise on treating non printable chars over ascii value 126 Case 1 : On some fields in the text , I need to retiain then 'as-is' and load to a database.I understand it also depends on database codepage. but i just wanna know how do i ensure it do not change while loading... (1 Reply)
Discussion started by: braindrain
1 Replies
Login or Register to Ask a Question