Doubt with rearranging file through awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Doubt with rearranging file through awk
# 1  
Old 11-19-2012
Doubt with rearranging file through awk

Code:
           Filename1.xml  NO  2012-11-16 02:00:27                   20121115/pathname/  asia
          Filename1.rec  YES  2012-11-16 01:20:24                    20121115/pathname 	asia
          FIleName2.xml  YES  2012-11-16 01:20:25                    20121115/pathaname asia

if the file content are properly filled, then below one works fine well with out any other fancy stuff like - or NOT ARRIVED YET
Code:
PS: a="02:00:00"
cat aa | awk -v var="$a" '{split($5,a,"/") ; printf ("%8d %15s %60s %10s %10s %5s\n", a[1],$6,$1,var,$4,$2)}'

so I have my desired o/p

Code:
20121115     asia                                                Filename1.xml   02:00:00   02:00:27    NO
20121115     asia                                                Filename1.rec   02:00:00   01:20:24   YES
20121115     asia                                                FIleName2.xml   02:00:00   01:20:25   YES


if the content changed a bit, in addition to the existing above records

Code:
FIleName3.xml   -                    -                          NOT ARRIVED YET 	asia
FileNamee3.rec  -                    -                          NOT ARRIVED YET 	asia
Filename1.xml   NO   2012-11-16 02:00:27                   	20121115/pathname/    	asia
Filename1.rec  YES  2012-11-16 01:20:24                    	20121115/pathname 	asia
FIleName2.xml  YES  2012-11-16 01:20:25                    	20121115/pathaname   	asia

Then how can I change have my desired o/p as below

Code:
20121115     asia                                                Filename3.xml   02:00:00   -    	-
20121115     asia                                                Filename3.rec   02:00:00   -   	-	
20121115     asia                                                Filename1.xml   02:00:00   02:00:27    NO
20121115     asia                                                Filename1.rec   02:00:00   01:20:24   YES
20121115     asia                                                FIleName2.xml   02:00:00   01:20:25   YES

PS: In the o/p file first column will be date which derived from PATH-NAME with DATE from input file and input file could have mix of NOT ARRIVED YET AND PATH with DATE values . PATH with DATE will be always same thru-out the file.
# 2  
Old 11-19-2012
Quote:
Originally Posted by manas_ranjan
my desired o/p as below

Code:
20121115     asia                                                Filename3.xml   02:00:00   -        -
20121115     asia                                                Filename3.rec   02:00:00   -       -    
20121115     asia                                                Filename1.xml   02:00:00   02:00:27    NO
20121115     asia                                                Filename1.rec   02:00:00   01:20:24   YES
20121115     asia                                                FIleName2.xml   02:00:00   01:20:25   YES

There is a one confusion.
From where above highlighted date should be printed??
# 3  
Old 11-19-2012
Hi Pamu,

the date column in output file has been inherited from the column PATHwithDATE inside input file.

Code:
NOT ARRIVED YET
NOT ARRIVED YET
20121115/pathname/
20121115/pathname/
20121115/pathname/

this particular column date with path will never change thru-out the file.

My suggestion,
So , we need to fetch first record where this column != "NOT ARRIVED YET" and then we need to fetch date as by split command in begin section for AWK and thru out the looping we need to call this date value for the first field.
# 4  
Old 11-19-2012
try

Code:
awk -v var="$a" 'NR==FNR{if($5 ~ /\//){split($5,a,"/");s=a[1]};next}{
if($0 ~ /NOT ARRIVED YET/){printf ("%8d %15s %60s %10s %10s %5s\n", s,$7,$1,var,$3,$2)}
else { printf ("%8d %15s %60s %10s %10s %5s\n", s,$6,$1,var,$4,$2)}}' file file


Last edited by pamu; 11-19-2012 at 06:17 AM.. Reason: corrected..
# 5  
Old 11-19-2012
it's working fine, other than date column which is coming out as 0 instead of
20121115

---------- Post updated at 05:14 AM ---------- Previous update was at 05:11 AM ----------

Code:
      0    asia                                                Filename1.xml   02:00:00   02:00:27    NO
       0     asia                                                Filename1.rec   02:00:00   01:20:24   YES
       0     asia                                                FIleName2.xml   02:00:00   01:20:25   YES
       0            asia                                                FIleName3.xml   02:00:00          -     -
       0            asia                                               FileNamee3.rec   02:00:00          -     -

---------- Post updated at 05:19 AM ---------- Previous update was at 05:14 AM ----------

I did something , it's somehow working, could you verify it
Code:
cat aa | awk -v var="$a" '{ if ($5 !~ /ARRIVED/) { if (NR==1) { split($5,a,"/"); date=a[1]} } {
> if($0 ~ /NOT ARRIVED YET/){printf ("%8d %15s %60s %10s %10s %5s\n", date,$7,$1,var,$3,$2)}
> else { printf ("%8d %15s %60s %10s %10s %5s\n",date,$6,$1,var,$4,$2)}}}'

Code:
my one question to you
on the same file, why  below o/p looks different
cat aa | awk '{ for (i=1;i<=NR;++i) {print i, $1}}'
1 Filename1.xml
1 Filename1.rec
2 Filename1.rec
1 FIleName2.xml
2 FIleName2.xml
3 FIleName2.xml
1 FIleName3.xml
2 FIleName3.xml
3 FIleName3.xml
4 FIleName3.xml
1 FileNamee3.rec
2 FileNamee3.rec
3 FileNamee3.rec
4 FileNamee3.rec
5 FileNamee3.rec

# 6  
Old 11-19-2012
i have corrected in my previous post. Please don't use hard coded syntax for any definition.

here see below.
NR - Line Number
NF - Number of fields

At start NR = 1 so loop runs only for 1 time. an so on loop increases as NR increases.(You may want to try with NF)

Code:
$ awk '{ for (i=1;i<=NR;++i) {print i, $1,NR}}' file
1 FIleName3.xml 1
1 FileNamee3.rec 2
2 FileNamee3.rec 2
1 Filename1.xml 3
2 Filename1.xml 3
3 Filename1.xml 3
1 Filename1.rec 4
2 Filename1.rec 4
3 Filename1.rec 4
4 Filename1.rec 4
1 FIleName2.xml 5
2 FIleName2.xml 5
3 FIleName2.xml 5
4 FIleName2.xml 5
5 FIleName2.xml 5

pamuSmilie
# 7  
Old 11-19-2012
thanks mate...
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help with rearranging file with script

Hi Guys I normally do thins with a Windows program but I am trying to rearrange a filename based on delimiters in Ubuntu. Example v017 __ Detective Academy Q #133 Murder in the Village Of Suspension Bridges &&& Part 9.cbz = Detective Academy Q v017 #133 Murder in the Village Of Suspension... (9 Replies)
Discussion started by: itschrisonline
9 Replies

2. Shell Programming and Scripting

Need Help in rearranging the content of a file comma seperated

I have a file with the below content a = test1 b = test2 a = test3 b= test4 c = test6 b = test5 d = test7 d = test9 Need the output to be as follows a = test1,test3 b = test2, test5 c = test6 d = test7, test9 (4 Replies)
Discussion started by: iron_michael86
4 Replies

3. Shell Programming and Scripting

Help with splitting and rearranging a field in awk

Please help! I figured I would take this one to the experts. I'm working with a field that contains contents such as: LastName FirstName For example: Smith John I'm trying to take this field and split it so that it is two separate fields (first name and last name). I then need to print... (5 Replies)
Discussion started by: IX3R0XI
5 Replies

4. Shell Programming and Scripting

Doubt on using AWK

DE_CODE|1{AXXANY}1APP_NAME|2{TELCO}2LOC|NY DE_CODE|1{AXXATX}1APP_NAME|2{TELCO}2LOC|TX DE_CODE|1{AXXABT}1APP_NAME|2{TELCO}2LOC|BT DE_CODE|1{AXXANJ}1APP_NAME|2{TELCO}2LOC|NJ i have out put file like below i have to convert it in the format as below. DE_CODE = AXXANY APP_NAME= TELCO LOC = NY... (4 Replies)
Discussion started by: mail2sant
4 Replies

5. Shell Programming and Scripting

Rearranging into new columns (awk?)

Hi experts, I've used several solutions from this forum to delete nonsense and rearrange data in the project file I'm working on. I'm hoping you guys can give me some tips on further rearranging the data (I've seen a few solutions by searching, but one specific item has me stumped, which is only... (5 Replies)
Discussion started by: coryvp
5 Replies

6. Shell Programming and Scripting

Rearranging the colums in a tab delimited file

I want to rearrange some of my columns in my dat file; how do i do this using a script Suppose, I have an input file like this: BASENAME STREETTYPE PREFIX SUFFIX HOUSENUMBER BUILDUP ORDER8 ORDER2 ORDER1 ISOCOUNTRYCODE POSTALCODE SILVER LAKE RD NW 1135 NEW BRIGHTON RAMSEY MINNESOTA USA 55112... (4 Replies)
Discussion started by: ramky79
4 Replies

7. Shell Programming and Scripting

awk file reading doubt

Hi, Using this trivial code, I am trying to insert/paste the single column data of a file into the second column (field 2) of a multi-column text file. awk 'FNR==NR {a=$0; next} {$1=$1 OFS a}1' single-column-file multi-column-file Lets consider the single-column-file as 'f2' and multi-column... (1 Reply)
Discussion started by: royalibrahim
1 Replies

8. Shell Programming and Scripting

Awk doubt

I have a file sample.txt with the following contents: the following gives output as awk 'NF{s=$0; print s}' sample.txt but, awk 'NF{s=$0}{print s}' sample.txtgives output as why this difference, can someone explain me? (6 Replies)
Discussion started by: royalibrahim
6 Replies

9. UNIX for Dummies Questions & Answers

doubt about awk

i have a file like this: awk.lst smith : sales : 1200 : 2 jones:it:25000 : 2 roger : it : 1500 : 2 ravi | acct | 15000 i have 3 doubts 1) when i say awk -F ":" '$2 ~ /'it'/ {print $0}' awk.lst i am not able to get jones in the ouput , is it because of space issue? 2)how to... (2 Replies)
Discussion started by: soujanya_srk
2 Replies

10. Shell Programming and Scripting

rearranging the data in file (from columnwise to rowwise)

Hi I have one file which is having data like 10201 10202 10205 10206 10207 10208 10209 10210 10211 10213 10215 10801 10802 11406 11415 11422 11426 11513 11514 11515 11516 11517 11518 11519 11520 11521 11522 11523 11524 11525 11530 11604 11608 11611 11717 11718 11719 11722 11725... (3 Replies)
Discussion started by: reldb
3 Replies
Login or Register to Ask a Question