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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reading multiple files 1

Status
Not open for further replies.

kasan001

Programmer
Apr 5, 2001
2
US
Hi All,

How do you read multiple files using awk. And how can we format the content of files to create a new output file.


Thanks
 

#!/bin/sh
newfile="/home/user/foo.txt"
awk_proc() {
echo -e " ` awk ' /regexp/ {
for (x=1 ; x <= NF ; x++)
print $x
}' `&quot;
return 0
}

for x in *
do
if [ -f &quot;$x&quot; ]
then

echo $x | awk_proc > $newfile
fi
done

This is about as general as you can get.
If you let us know what patterns you are looking for,etc..
We could help more succinctly.

Also sort is very powerful for this kind of work:
cat * | sort -options can work just as well or better than
an awk script sometimes.
 

Hi, kasan!

> How do you read multiple files using awk?

awk operates on one or more input files. You can specify input files in command line with
1.1 filename, for example:

awk -f script.awk inputfile.txt

1.2 list, for example:

awk -f script.awk file1.txt file2.txt file3.txt

1.3 wildcards

awk -f script.awk *.txt

*.txt means: all files with .txt extension.

You can specify any number of input files on the command line and awk will automatically open and read them. This is an attribute of scripting programming langaugage: awk automates processing command-line arguments - awk will automatically get the filename from the command line, open a file, and read its contents line-by-line. This is really powerful. I think, awk is also a simpler to use alternative to Perl, Python and Tcl.

Bye!

KP.
 
Thank you everybody.

Thanks for the information. Let me explain what I am actually looking for.

I have two input files. File1.txt and Filie2.txt.

File1.txt contains statics information.
File2.txt contains dynamics information generated by other shell scripts.

I intend to use awk script so that I can add all the static info from File1.txt to each line of File2.txt and write it a new output file.

For ex.

File1.txt is like this

name
country
phone #
zip code


File2.txt is like this

field1\t field2\t field3\t field4

many such rows.


output file which i am lookin is like this

name/tcountry/tphone #/t zip code/t field1\t field2\t field3\t field4

If any one of you could help that would be great!
 
Hi kasan001,

Not sure about output format you want
exactly, but this duplicates what you
posted to this forum.


awk '

FILENAME == &quot;File1.txt&quot; {
prefix[++i] = $0
}

FILENAME == &quot;File2.txt&quot; {
line= sprintf (&quot;%s/%s/t%s/t %s/t %s\n&quot;, prefix[1],prefix[2],prefix[3],prefix[4],$0)
print line

}' File1.txt File2.txt > NewFile


Hope this is close to your needs!



flogrr
flogr@yahoo.com

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top