1847337 Members
2216 Online
110264 Solutions
New Discussion

Awk scripting

 
SOLVED
Go to solution
Oktay Tasdemir
Advisor

Awk scripting

Hi,

I need to extract the first occurrence of the field ABNA which is located on line 13 of the attached file and rename the file using this field. i.e test.ABNA.file

Could you please assist.

Many thanks
Oktay
Let the fun and games begin
10 REPLIES 10
curt larson_1
Honored Contributor

Re: Awk scripting

cat test |
awk '/ABNA/ {print $0;exit;}' > test.ABNA.file
Oktay Tasdemir
Advisor

Re: Awk scripting

Thanks for the reply,
The field where ABNA is located is variable and it denotes a company name which changes all the time.

Sorry I should have been more explicit.
Let the fun and games begin
curt larson_1
Honored Contributor

Re: Awk scripting

yes some additional information would be helpful
Fragon
Trusted Contributor

Re: Awk scripting

Hi Oktay,
From the sample file you posted,I found the the first occurrence of the filed "ABNA" is always exists in the line begins with "BHP".
So, this may help:
fnname=`more test|sed -n '/BHP/p'|awk '{print $8}'`
mv test test.${fnname}.file

Hope it can help you!

-ux
Fragon
Trusted Contributor

Re: Awk scripting

If you prefer that line 13, the script should be:
fnname=`more test|sed -n '13p`|awk '{print $1}'`

Good luck!
-ux
Sridhar Bhaskarla
Honored Contributor

Re: Awk scripting

Hi Oktay,

Line 13th is fixed. Is the field 8th in 13th line fixed too?.

In that case you can simply use these two lines.

FIELD=$(sed -n '13p' your_file|awk '{print $8}')
cp your_file test.${FIELD}.file

-Sri
You may be disappointed if you fail, but you are doomed if you don't try
Oktay Tasdemir
Advisor

Re: Awk scripting

Thanks for your replies,

Line 13 is fixed however the 8th variable is not and depending on the file it changes?
Let the fun and games begin
Francisco J. Soler
Honored Contributor

Re: Awk scripting

Hi Oktay,
If the 8th field in the 13th line is not fixed, how can we isolated the company name?

You must supply some condition to detect this name in order to extract it.

Frank.
Linux?. Yes, of course.
Ionut Grigorescu_2
Super Advisor
Solution

Re: Awk scripting

Hi Oktay,

try this script:

#!/bin/sh
export file_name=`awk '{if (NR==13) {for (i=1;i<=NF;i++) {if($i != ""){print $i; next}}}}' $1`
mv $1 "$file_name"

than you can use it like
#<script_name>

it will rename your file to ABNA
awk will search record number 13 for the first non-empty field(in this case ABNA), then will print this field and exit the for loop.
you can change the last line to
mv $1 test. "$file_name".txt if you like.
Not so short but it works.

Have fun, ionut
If it weren't for STRESS I'd have no energy at all
Michael Kelly_5
Valued Contributor

Re: Awk scripting

Oktay,
in the file you attached there is no ABNA string in line 13 but it does appear twice in line 11.
I'm assuming that the string you want to extract is the last non-blank field in line 11.
If this is the case, you can do this:

#!/usr/bin/sh

if [ ! -e $1 ]; then
echo "File $1 does not exist. Exiting"
exit 1
fi

newname=`awk 'NR == 11 { sub("[\t ]+\015$", ""); print $NF}' $1`
[ ! -z $newname ] && mv $1 test.${newname}.file

The awk bit strips the trailing whitespace and CR from line 11 and then prints the last field.
If you want the second last field, change $NF to $(NF - 1)

HTH,
Michael.
The nice thing about computers is that they do exactly what you tell them. The problem with computers is that they do EXACTLY what you tell them.