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

Creating a new file from other files... 3

Status
Not open for further replies.

gabrielAllen

Programmer
Feb 14, 2001
19
0
0
US
I have an infinite number of incoming files that need to be reformatted and combined into one file. Combining them is easy. My problem I am getting a "|" delimited file of 2 fields, vendor# and vendor name, that I need to create a fixed width record with and be able to add a date to the end of it.

ex.(Incoming)
1234567|McBurgers
*Vendor is (7) and Name is (30).

I also need to write a SYS date for at the 38th position of the, new, fixed width record. So, it should llok like this after the example above is processed:
"1234567McBurgers SPCA10/07/2004"

Any help would be greatly appreciated. Thanks!

Ken

 
something like this ?
awk -F'|' -v d=$(date "+%m/%d/%Y") '{printf "%7.7s%30.30s%s\n",$1,$2,d}' /path/to/input >output

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Thanks! Can you explain what is going on? 7.7s? Also, How do I get a 4 character constant in front of the date? The output should be:

123457McBurgers SPCA(Date)



Thanks again!
Ken
 
p.s. I need to left justify the vendor name... Thanks!
 
try this variation on the theme:

Code:
nawk -F'|' -v OFS="" -v d=$(date "+%m/%d/%Y") '{$1=$1; printf "%-37.37sSPCA%s\n",$0,d}' /path/to/input >output

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Save this program in a file named "fixedformat.awk":
Code:
BEGIN{ FS="|" }

{ printf "%-7s%-30sSPCA%s\n", $1, $2, d }

Run it with:
[tt]
awk -v d=$(date "+%m/%d/%Y") -f fixedformat.awk inputfile*.dat >outfile
[/tt]

In a print-formatted command, [tt]%s[/tt] means to print a string.
Example:
[tt]
printf "The %s was too old.\n", "horse"
The horse was too old.
[/tt]
[tt]%15s[/tt] means to print a string with a width of 15:
[tt]
printf "The %15s was too old.\n", "horse"
The horse was too old.
[/tt]
[tt]%-15s[/tt] means to print a string left-justified in a width of 15:
[tt]
printf "The %-15s was too old.\n", "horse"
The horse was too old.
[/tt]

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top