Help with awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with awk
# 1  
Old 04-10-2013
IBM Help with awk

Hi,

I have a stream of data like below:
Code:
abcdef1CXYZ1999PQR
ghijkl2MNOJ2012GHI

By using AWK and substr I am extracting the data, but I need to perform a substr based on the year. Say if Year is less than 2011 then I have to extract 9th char for output 3rd column and if Year is greater than or equal 2011, I have to extract 11th char for output 3rd column.

Code:
abc|def|C|1999
ghi|jkl|J|2012

Thanks.
# 2  
Old 04-10-2013
Code:
awk '{X=11} substr($0,12,4)<2011{X=8}
    {print substr($0,1,3),substr($0,4,3),substr($0,X,1),substr($0,12,4)}' OFS="|" file

# 3  
Old 04-10-2013
Unfortunately, your written request is not consistent with the sample you gave (Pos 9 as opposed to Pos 8). Try this, based on your written request:
Code:
$ awk  '{YR=$12$13$14$15; print $1$2$3, $4$5$6, YR<2012?$9:$11, YR}' FS="" OFS="|" file
abc|def|X|1999
ghi|jkl|J|2012

# 4  
Old 04-10-2013
IBM

Let me tell in a clear way....
I have input file as:
Code:
C0000169811J4GL48K83W6854582003-11-19KJJH74200321796572863292268120001-01-01NY1NN 0001-01-01NN2012-10-15+0017.2O+0008.1N018861244178   60  NN11            001
C0001583911B7FN14X6JS7417842006-09-15 N1L61198813156468100000676860001-01-01UY1NN 0001-01-01YN2006-11-10+0000.0A+0008.2N106031946596   00  NN97            001
C0001583911C3EJ56H5SN5823592007-01-08JACP41199500680246900000676860001-01-01UY1NN 0001-01-01YN2007-03-09+0000.0A+0008.2N106031946596   00  NN97            001
C0005698011C3CDFBH5DD1580052012-10-19PFDP41201328423902466684666840001-01-01NY1NN 0001-01-01NN0001-01-01+0020.4 +0020.4N357526219604   00  NN01            001

My code is like below:
Code:
 awk 'BEGIN {OFS="\t"} {if  (substr($0,79,1) ~ "1" || substr($0,79,1) ~ "C" || substr($0,79,1) ~ "L" || substr($0,79,1) ~ "B" || substr($0,
79,1) ~ "E") print substr($0,02,09),
        substr($0,48,09),
        substr($0,28,10),
        substr($0,62,05),
        substr($0,67,10),
        substr($0,12,01),
        substr($0,93,01)}'

The above code is working fine, but the requirement is changed in retrieving substr($0,12,01), which is last but one :

Code:
{if (substr($0,28,4)  > 2011)
 print substr($0,15,01)
 else
 print substr($0,12,01)};

Need to integrate this functionality in the existing,
i.e; print with if condition

---------- Post updated at 11:07 PM ---------- Previous update was at 05:25 PM ----------

Is it possible to implement if-else condition in print stmt ?
# 5  
Old 04-10-2013
Try (untested):
Code:
$ awk 'BEGIN  {OFS="\t"}
              {YR = substr($0,28,4), X = substr($0,79,1)}
       X ~ /[1CLBE]/ {print substr($0,02,09), 
               ...,
               substr($0, YR>2011?15:12, 1),
               ...}
       '

# 6  
Old 04-11-2013
IBM

Quote:
Originally Posted by RudiC
Try (untested):
Code:
$ awk 'BEGIN  {OFS="\t"}
              {YR = substr($0,28,4), X = substr($0,79,1)}
       X ~ /[1CLBE]/ {print substr($0,02,09), 
               ...,
               substr($0, YR>2011?15:12, 1),
               ...}
       '

Syntax error:
awk: cmd. line:1: {YR = substr($0,28,4), X = substr($0,79,1)}
awk: cmd. line:1: ^ syntax error

I am trying as follows:

INPUT_FILE:
Code:
C0000169811J4GL48K83W6854582003-11-19KJJH74200321796572863292268120001-01-01NY1NN 0001-01-01NN2012-10-15+0017.2O+0008.1N018861244178   60  NN11            001
C0001583911B7FN14X6JS7417842006-09-15 N1L61198813156468100000676860001-01-01UY1NN 0001-01-01YN2006-11-10+0000.0A+0008.2N106031946596   00  NN97            001
C0001583911C3EJ56H5SN5823592007-01-08JACP41199500680246900000676860001-01-01UY1NN 0001-01-01YN2007-03-09+0000.0A+0008.2N106031946596   00  NN97            001
C0005698011C3CDFBH5DD1580052012-10-19PFDP41201328423902466684666840001-01-01NY1NN 0001-01-01NN0001-01-01+0020.4 +0020.4N357526219604   00  NN01            001

AWK:
Code:
cat INPUT_FILE | \
awk 'BEGIN {OFS="\t"} {if  (substr($0,79,1) ~ "1" || substr($0,79,1) ~ "C" || substr($0,79,1) ~ "L" || substr($0,79,1) ~ "B" || substr($0,
79,1) ~ "E") print substr($0,02,09),
        substr($0,48,09),
        substr($0,28,10),
        substr($0,62,05),
        substr($0,67,10),
 
{ if (substr($0,28,4)  > 2011)
 print substr($0,15,01)
 else
 print substr($0,12,01) };
 
        substr($0,93,01)}'

How to implement the if in PRINT, in AWK?
# 7  
Old 04-11-2013
Replace , with ; here:
Code:
           {YR = substr($0,28,4), X = substr($0,79,1)}
                                ^--- ";"

This User Gave Thanks to RudiC For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk output yields error: awk:can't open job_name (Autosys)

Good evening, Im newbie at unix specially with awk From an scheduler program called Autosys i want to extract some data reading an inputfile that comprises jobs names, then formating the output to columns for example 1. This is the inputfile: $ more MapaRep.txt ds_extra_nikira_usuarios... (18 Replies)
Discussion started by: alexcol
18 Replies

2. Shell Programming and Scripting

Pass awk field to a command line executed within awk

Hi, I am trying to pass awk field to a command line executed within awk (need to convert a timestamp into formatted date). All my attempts failed this far. Here's an example. It works fine with timestamp hard-codded into the command echo "1381653229 something" |awk 'BEGIN{cmd="date -d... (4 Replies)
Discussion started by: tuxer
4 Replies

3. Shell Programming and Scripting

Passing awk variable argument to a script which is being called inside awk

consider the script below sh /opt/hqe/hqapi1-client-5.0.0/bin/hqapi.sh alert list --host=localhost --port=7443 --user=hqadmin --password=hqadmin --secure=true >/tmp/alerts.xml awk -F'' '{for(i=1;i<=NF;i++){ if($i=="Alert id") { if(id!="") if(dt!=""){ cmd="sh someScript.sh... (2 Replies)
Discussion started by: vivek d r
2 Replies

4. Shell Programming and Scripting

HELP with AWK one-liner. Need to employ an If condition inside AWK to check for array variable ?

Hello experts, I'm stuck with this script for three days now. Here's what i need. I need to split a large delimited (,) file into 2 files based on the value present in the last field. Samp: Something.csv bca,adc,asdf,123,12C bca,adc,asdf,123,13C def,adc,asdf,123,12A I need this split... (6 Replies)
Discussion started by: shell_boy23
6 Replies

5. Shell Programming and Scripting

awk command to compare a file with set of files in a directory using 'awk'

Hi, I have a situation to compare one file, say file1.txt with a set of files in directory.The directory contains more than 100 files. To be more precise, the requirement is to compare the first field of file1.txt with the first field in all the files in the directory.The files in the... (10 Replies)
Discussion started by: anandek
10 Replies

6. Shell Programming and Scripting

Comparison and editing of files using awk.(And also a possible bug in awk for loop?)

I have two files which I would like to compare and then manipulate in a way. File1: pictures.txt 1.1 1.3 dance.txt 1.2 1.4 treehouse.txt 1.3 1.5 File2: pictures.txt 1.5 ref2313 1.4 ref2345 1.3 ref5432 1.2 ref4244 dance.txt 1.6 ref2342 1.5 ref2352 1.4 ref0695 1.3 ref5738 1.2... (1 Reply)
Discussion started by: linuxkid
1 Replies

7. 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

8. Shell Programming and Scripting

awk: assign variable with -v didn't work in awk filter

I want to filter 2nd column = 2 using awk $ cat t 1 2 2 4 $ VAR=2 #variable worked in print $ cat t | awk -v ID=$VAR ' { print ID}' 2 2 # but variable didn't work in awk filter $ cat t | awk -v ID=$VAR '$2~/ID/ { print $0}' (2 Replies)
Discussion started by: honglus
2 Replies

9. Shell Programming and Scripting

scripting/awk help : awk sum output is not comming in regular format. Pls advise.

Hi Experts, I am adding a column of numbers with awk , however not getting correct output: # awk '{sum+=$1} END {print sum}' datafile 2.15291e+06 How can I getthe output like : 2152910 Thank you.. # awk '{sum+=$1} END {print sum}' datafile 2.15079e+06 (3 Replies)
Discussion started by: rveri
3 Replies

10. Shell Programming and Scripting

Awk problem: How to express the single quote(') by using awk print function

Actually I got a list of file end with *.txt I want to use the same command apply to all the *.txt Thus I try to find out the fastest way to write those same command in a script and then want to let them run automatics. For example: I got the file below: file1.txt file2.txt file3.txt... (4 Replies)
Discussion started by: patrick87
4 Replies
Login or Register to Ask a Question