Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Adding data to a flat file from ksh (maybe using awk) 2

Status
Not open for further replies.

RayC87

Programmer
Oct 29, 2001
5
GB
This is all new to me so any help would be greatly appreciated

I need to be able to add a value to a specific line of a data file.

The file looks like:

a 123 345789
b 234 678909 iuy
b 234 678909 jhg
b 234 678909 nbv
c 321 097876

And I need to add the filename to the row starting with 'c'(the data can be in any order)
i.e.
a 123 345789
b 234 678909 iuy
b 234 678909 jhg
b 234 678909 nbv
c 321 097876 filename

Any help would be appreciated

Regards

Ray
 

Hi, RayC87!

You can try this awk example:

Code:
awk '$1 == "c"  { print $0 " " FILENAME }
$1 != "c"  { print }' inputfile > outputfile

Bye!

KP.
 

Hi again, RayC87!

Previous awk example uses relational expressions as patterns. Here is an example with regular expressions:

Code:
awk '/^c/  { print $0 " " FILENAME }
/^[^c]/  { print }'  inputfile

I hope this works.

Bye!

KP.
 
KP,

Thanks alot that has been really helpful.

FILENAME adds the full path along with the actual file name into the file could you give me an example where the filename is passed to awk via a ksh variable.

I appreciate your help with this as I have already wasted half a day trying to sort this out myself!

Thanks again

Ray
 
I know I'm butting in here Krunek, but in the interests of a quick response ...

awk '/^c/ { print $0 " " varfile }
/^[^c]/ { print }' varfile=$FILE_NAME inputfile

Greg :)

 
Greg,

Thanks for that. I'm off to buy myself an AWK reference manual!

This has been my first time using tec-tips and it's really helped.

Thanks again

Ray :->
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top