cut & AWK


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting cut & AWK
# 1  
Old 06-14-2011
cut & AWK

HI all,

i have the source file like below,

05 BL-FEE-CYC-CDE PIC S9(03) COMP-3.
05 BL-FEE-ERR-MSG PIC X(00030).
05 BL-FEE-TYPE PIC X(00001).
418181*# 05 BL-LCYC-REL-CTD-AVG PIC S9(15) COMP-3.
418181 05 BL-LCYC-REL-CTD-AVG PIC S9(15)V9(02) COMP-3.
05 BL-LCYC-REL-CTD-AVG PIC S9(15)V9(02) COMP-3. ##define

i need to cut the previous column of "PIC" column and 2 columns after PIC.

in a row PIC may come in any column in the above 1st 3 rows its coming in 3rd column
in 4th and 5th rows it comes in 4th column

lost row it comes in 3rnd column.

i tried with AWK.

can some one help me to find the column id of PIC

Thanks
Baski
# 2  
Old 06-14-2011
give the desired output of your example
# 3  
Old 06-14-2011
This works, make sure you have two columns after pic to avoid bad results,


Code:
awk '{for(i=1;i<=NF;i++){if($i=="PIC"){print $(i-1) $(i+1) $(i+2)}}} ' test.txt

This User Gave Thanks to kumaran_5555 For This Post:
# 4  
Old 06-14-2011
Code:
awk '{for(i=1;i<=NF;i++){if($i=="PIC")print $(i-1) $(i+1) $(i+2)} }' inputfile

# 5  
Old 06-14-2011
Thanks a lot Kurmar..

I hope it will work..let me check..

Baski
# 6  
Old 06-14-2011
@baskivs

Quote:
Originally Posted by baskivs
HI all,

i have the source file like below,

05 BL-FEE-CYC-CDE PIC S9(03) COMP-3.
05 BL-FEE-ERR-MSG PIC X(00030).
05 BL-FEE-TYPE PIC X(00001).
418181*# 05 BL-LCYC-REL-CTD-AVG PIC S9(15) COMP-3.
418181 05 BL-LCYC-REL-CTD-AVG PIC S9(15)V9(02) COMP-3.
05 BL-LCYC-REL-CTD-AVG PIC S9(15)V9(02) COMP-3. ##define

i need to cut the previous column of "PIC" column and 2 columns after PIC.

in a row PIC may come in any column in the above 1st 3 rows its coming in 3rd column
in 4th and 5th rows it comes in 4th column

lost row it comes in 3rnd column.

i tried with AWK.

can some one help me to find the column id of PIC

Thanks
Baski
Please always use code tags icon available in the menu bar while posting 'codes,sample data,sample output etc' so that it helps improving the readability
Code:
sed 's/.* \([^ ]*\) PIC \([^ ]* *[^ ]*\) *.*/\1 \2/' inputfile > outfile

# 7  
Old 06-14-2011
Try this,
Code:
nawk '{for(i=1;i<=NF;i++) { j=i+2; k=i-2; if($i=="PIC") print $k" "$j } }' inputfile


Last edited by Franklin52; 06-14-2011 at 08:07 AM.. Reason: Please use code tags, thank you
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Cut & awk

I am using the following code to modify all odd lines in a file: awk 'NR % 2 { print } !(NR % 2)' FWD-1.fas | cut -c5-600 I however, do not want cut to affect the odd lines Any help? (3 Replies)
Discussion started by: Xterra
3 Replies

2. Shell Programming and Scripting

Cut & Fetch word from string

I have a file with some SQL query, I want to fetch only Table Name from that file line by line. INPUT FILE SELECT * FROM $SCHM.TABLENAME1; ALTER TABLE $SCHM.TABLENAME1 ADD DateOfBirth date; INSERT INTO $SCHM.TABLENAME1 (CustomerName, Country) SELECT SupplierName, Country FROM $SCHM.TABLENAME2... (2 Replies)
Discussion started by: Pratik Majithia
2 Replies

3. Homework & Coursework Questions

sed & cut command issues

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: After using the egrep command to pull certain lines from the asg5f1 (creating the asg5f1c file), I am required... (1 Reply)
Discussion started by: robrom78
1 Replies

4. Shell Programming and Scripting

Advice using cut & echo combination commands

Hi, I am cutting data from a fixed length test file and then writing out a new record using the echo command, the problem I have is how to stop multiple spaces from being written to the output file as a single space. Example: cat filea | while read line do field1=`echo $line | cut -c1-2` ... (6 Replies)
Discussion started by: dc18
6 Replies

5. Shell Programming and Scripting

storing output from echo & cut into variable

Hi All, Hope someone can advise here as I have been struggling to find a syntax that works here. I have tried a stack of combination I have seed in the forums but I think because I have needed to use "" and `` in the statments another method is found. I am reading in lines with the following... (1 Reply)
Discussion started by: nkwilliams
1 Replies

6. UNIX for Dummies Questions & Answers

cmd sequence to find & cut out a specific string

A developer of mine has this requirement - I couldn't tell her quickly how to do it with UNIX commands or a quick script so she's writing a quick program to do it - but that got my curiousity up and thought I'd ask here for advice. In a text file, there are some records (about half of them)... (4 Replies)
Discussion started by: LisaS
4 Replies

7. Shell Programming and Scripting

grep & cut the file

I need to grep and cut the some file in the folder and output to some file. the sample file looks like below -rw-r--r-- 1 gui gui 28050789 Jun 25 18:38 mymkzpiii.txt -rw-r--r-- 1 gui gui 127983856 Jun 25 18:39 phmnlpiii.txt i need the output like below ... (3 Replies)
Discussion started by: aboorkuma
3 Replies

8. Shell Programming and Scripting

awk & cut record separator problem

Hi All, I've got some strange behaviour going on when trying to manipulate a file that contains spaces. My input file looks something like this: xxxxxxxxx,yyyy,sss sss sss,bbbbbbb If I use awk: When running from the command line I get: sss sss sss But when running from a... (7 Replies)
Discussion started by: pondlife
7 Replies

9. Shell Programming and Scripting

cut & paste

hi i am new to shell scripting, i have been trying to cut columns numbered 1,4 of a file consisiting of 4 columns. Each column is seperated by 2 spaces. for example: john 6102097199 tennessee usa michel 6734590899 texas USA now, i need to cut the name... (3 Replies)
Discussion started by: t_harsha18
3 Replies
Login or Register to Ask a Question