Using awk and output to files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Using awk and output to files
# 1  
Old 01-11-2013
Using awk and output to files

I currently have this code:

Code:
awk ' BEGIN { FS = OFS = "|"} { l=length($1) $1 = sprintf("%s-%s-%s %s:%s:%s", substr($1, l - 13, 4), substr($1, l - 9, 2), substr($1, l - 7, 2), substr($1, l - 5, 2), substr($1, l - 3, 2), substr($1, l - 1)) print }' infile  exit

Lets say I also wanted to modify $11 in a different format, where would I begin that statement?

Also what does infile actually represent. I replaced "infile" with a specific file that I wanted the code to run on, but what if I wanted to run the code across a whole directory, or set of files?

Last edited by joeyg; 01-11-2013 at 03:54 PM.. Reason: removed extra spaces in command
# 2  
Old 01-11-2013
infile represent the file to be accessed; so you are correct

To work with a directory would require you creating a loop around that statement. You should do a search in this forum for loop, or do while
# 3  
Old 01-11-2013
Code:
awk ' BEGIN { FS = OFS = "|"}
{ l=length($1)
  $1 = sprintf("%s-%s-%s %s:%s:%s", substr($1, l - 13, 4), substr($1, l - 9, 2), substr($1, l - 7, 2), substr($1, l - 5, 2), substr($1, l - 3, 2), substr($1, l - 1))
 $11 = ...
 print }' infile

infile is the input file, yes. The output gets printed to standard output -- i.e. the terminal, unless you redirect it somewhere else with > filename.

Last edited by Corona688; 01-11-2013 at 04:36 PM.. Reason: fix typo
# 4  
Old 01-11-2013
Thank to the both of you. I will attempt to give it a shot. Likely I'll be back. Smilie

-David

---------- Post updated at 04:05 PM ---------- Previous update was at 03:10 PM ----------

Quote:
Originally Posted by Corona688
Code:
awk ' BEGIN { FS = OFS = "|"}
{ l=length($1)
  $1 = sprintf("%s-%s-%s %s:%s:%s", substr($1, l - 13, 4), substr($1, l - 9, 2), substr($1, l - 7, 2), substr($1, l - 5, 2), substr($1, l - 3, 2), substr($1, l - 1))
 $11 = ...
 print }' infile

infile is the input file, yes. The output gets printed to standard output -- i.e. the terminal, unless you redirect it somewhere else with > filename.

Corona,

What if I wanted to keep $1 as it is in the code and have an x amount of spaces or a tab between each delimiter, how would that look and where would that statement start?
# 5  
Old 01-15-2013
Quote:
Originally Posted by LDHB2012
What if I wanted to keep $1 as it is in the code and have an x amount of spaces or a tab between each delimiter, how would that look and where would that statement start?
I'm not really following you.

Please show the input you have, and the output you want.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk output different between two files

The awk below when run using the contents of file, works great with the desired output of expName barcodeSampleInfo barcodedSamples . However, when the complete file is used (attached) I get different output. It looks like the same data is there but the ordering is off. Both data sets are... (2 Replies)
Discussion started by: cmccabe
2 Replies

2. Shell Programming and Scripting

Using awk to output matches between two files to one file and mismatches to two others

I am trying to output the matches between $1 of file1 to $3 of file2 into a new file match. I am also wanting to output the mismatches between those same 2 files and fields to two separate new files called missing from file1 and missing from file2. The input files are tab-delimited, but the... (9 Replies)
Discussion started by: cmccabe
9 Replies

3. Shell Programming and Scripting

Remove lines from output in files using awk

I have two large files (~250GB) that I am trying to remove the where GT: 0/0 or 1/1 or 2/2 for both files. I was going to use a bash with the below awk, which I think will find each line but how do I remove that line is that condition is found? Thank you :). Input 20 60055 . A ... (4 Replies)
Discussion started by: cmccabe
4 Replies

4. Shell Programming and Scripting

awk: too many output files created from while loop

I am using awk to read lines from a CSV file then put data into other files. These other files are named using the value of a certain column. Column 7 is a name such as "att" or "charter" . I want to end up with file names with the value of column 7 appended to them, like this: ... (5 Replies)
Discussion started by: dodgerfan78
5 Replies

5. Shell Programming and Scripting

Compare two files and output difference, by first field using awk.

It seems like a common task, but I haven't been able to find the solution. vitallog.txt 1310,John,Hancock 13211,Steven,Mills 122,Jane,Doe 138,Thoms,Doe 1500,Micheal,May vitalinfo.txt 12122,Jane,Thomas 122,Janes,Does 123,Paul,Kite **OUTPUT** vitalfiltered.txt 12122,Jane,Thomas... (2 Replies)
Discussion started by: charles33
2 Replies

6. UNIX for Dummies Questions & Answers

df -> output files; comparison using awk or...

:wall: I am trying to do the following using awk (is that the best way?): Read 2 files created from the output of df (say, on different days) and compare the entries using the 1st (FileSys) and 6th (Mount) fields to see if the size has changed. Output (at least), to a new file (some header... (2 Replies)
Discussion started by: renata
2 Replies

7. Shell Programming and Scripting

output 2 files using awk

Hi guys, Basically, I have an END {print NR} statement in my awk script to count the number of records as I have concatenated multiple files. But instead of generating this total number of records to the same output data file. I want to put this in a separate control file using the same awk... (6 Replies)
Discussion started by: Det7
6 Replies

8. Shell Programming and Scripting

AWK Compare files, different fields, output

Hi All, Looking for a quick AWK script to output some differences between two files. FILE1 device1 1.1.1.1 PINGS device1 2.2.2.2 PINGS FILE2 2862 SITE1 device1-prod 1.1.1.1 icmp - 0 ... (4 Replies)
Discussion started by: stacky69
4 Replies

9. Shell Programming and Scripting

Redirecting to different output files with awk.

Well, it didn't take me long to get stumped again. I assure you that I'm not mentally deficient, just new to scripting. So, here's the gist. I want to redirect output from awk based off of which branch of an if-else statement under which it falls. #!/bin/bash #some variables... (2 Replies)
Discussion started by: mikesimone
2 Replies

10. Shell Programming and Scripting

Writing output into different files while processing file using AWK

Hi, I am trying to do the following using AWK program. 1. Read the input data file 2. Parse the record and see if it contains errors 3. If the record contains errors, then write it into Reject file, else, write into usual output file or display it on the screen Here is what I have done -... (6 Replies)
Discussion started by: vidyak
6 Replies
Login or Register to Ask a Question