field substitution w/awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting field substitution w/awk
# 1  
Old 06-06-2003
field substitution w/awk

I want to replace columns 15 thru 22 with a date in this format mmddyyyy in a file that has fixed record length of 110 columns. Only lines with column 1 = "T" will be changed but I can't seem to get it to work.

I saw several postings and tried to work with them but they don't work for me...

If I had 20 lines, any lines not 'T' is echoed out as is and when there's a 'T', it is supposed to substitute 15-22 with, say 06062003, but awk command substr won't do it:

echo "$LINE" | awk '{print substr ($0, 1, 14) $proc_dt substr ($0, 24, length ($0) - 24)}' >> newfile

it's literally printing $proc_dt in the newfile...when it should say 06062003 in col 15-22 (I've already set the variable...)

What's going on? I tried enclosing $proc_dt in (, {, etc and nothing works...

Gianni
# 2  
Old 06-06-2003
You have something like this:

'blah blah blah ${variable} more stuff'

The whole idea of single quotes is that "what you see is what you get". The shell will not make any substitutions inside single quotes.

So turn that into two single quoted strings with the variable in between. Like this:

'blah blah blah '${variable}' more stuff'

The brackets are not strictly needed, but I think they improve the readability of the code.
# 3  
Old 06-09-2003
Awesome. I thought I tried this already and it didn't work before...

In any case. this works as expected, so thank you.

The other question comes back to calling the external awk program which seems really slow when trying to substitute 20K records (could take 10-15 minutes) depending on how many users are on the system. Does anyone have any other ideas on how this could be achieved quicker? sed?

Gianni
# 4  
Old 06-09-2003
If you have a shell while loop, and within it you are doing:

echo "$LINE" | awk ...

then you are calling awk 20,000 times, opening your output file 20,000 times etc. Instead, do away with the shell loop, and do it with awk:
Code:
awk '{
if ($1 != "T")
   print
else
   print substr ...
}' infile > newfile

# 5  
Old 06-10-2003
A bit more flexible than the other solutions:
Code:
awk -v newdate=06062003 '/^T/ {
        $0 = substr($0,1,14) newdate substr($0,23,length($0)-22)
    }
    { print $0 }'

This can be put in a shell script to automatically get the date from the 'date' command (if you want today's date), or your script can check the date to be sure it's valid (or you can check the validity in the awk ...) ...

BTW, your original solution dropped a character from the beginning of the line after what comes after the date and another from the end of the line in the expression "substr ($0, 24, length ($0) - 24)".
# 6  
Old 06-13-2003
awk '{
if (substr($1, 1, 1) != 'T' )
print
else
print substr ($1, 1, 14) '${proc_dt}' substr ($1, 24, length ($1))}'
}' infile > newfile

What's wrong with this? I'm not good w/awk..
I got it to work with the while statement but not this way...

Gianni
# 7  
Old 06-14-2003
That's not bad, actually - just a couple of issues ...
Code:
awk '{
if (substr($1, 1, 1) != "T" )
print
else
print substr ($1, 1, 14) '\"${proc_dt}\"' substr ($1, 24)
}' infile > newfile

Your code terminates with two rightbrace-quotemarks, so it hangs waiting on stdin because it does not see infile beyond that second quote.

There are several ways to get a variable into awk. The approach you used by allowing it to be substituted when the command line is parsed is a good way. However, when awk sees the unprotected 14-JUN-2003, that is evaluated as a numeric expression. JUN, as an undefined variable, evaluates to zero, so that makes 14-0-2003 = -1989. You want awk to see that as a string constant. So I surrounded it with double quotes and protected with backslashes so they don't get removed by the shell.

And a small point: When you want the tail-end of a word, such as beginning with character 24 for the remainder of that word, you can just say substr($1,24). With no third operand, it takes the remainder of the expression being substringed.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Problem with getting awk to multiply a field by a value set based on condition of another field

Hi, So awk is driving me crazy on this one. I have searched everywhere and read man, docs and every related post Google can find and still no luck. The actual files I need to run this on are sensitive in nature, but it is the same thing as if I needed to calculate weighted grades for multiple... (15 Replies)
Discussion started by: cotilloe
15 Replies

2. Shell Programming and Scripting

awk to update field using matching value in file1 and substring in field in file2

In the awk below I am trying to set/update the value of $14 in file2 in bold, using the matching NM_ in $12 or $9 in file2 with the NM_ in $2 of file1. The lengths of $9 and $12 can be variable but what is consistent is the start pattern will always be NM_ and the end pattern is always ;... (2 Replies)
Discussion started by: cmccabe
2 Replies

3. Shell Programming and Scripting

awk to adjust coordinates in field based on sequential numbers in another field

I am trying to output a tab-delimited result that uses the data from a tab-delimited file to combine and subtract specific lines. If $4 matches in each line then the first matching sequential $6 value is added to $2, unless the value is 1, then the original $2 is used (like in the case of line... (3 Replies)
Discussion started by: cmccabe
3 Replies

4. Shell Programming and Scripting

How can awk ignore the field delimiter like comma inside a field?

We have a csv file as mentioned below and the requirement is to change the date format in file as mentioned below. Current file (file.csv) ---------------------- empname,date_of_join,dept,date_of_resignation ram,08/09/2015,sales,21/06/2016 "akash,sahu",08/10/2015,IT,21/07/2016 ... (6 Replies)
Discussion started by: gopal.biswal
6 Replies

5. Shell Programming and Scripting

awk to parse field and include the text of 1 pipe in field 4

I am trying to parse the input in awk to include the |gc= in $4 but am not able to. The below is close: awk so far: awk '{sub(/\|]+]++/, ""); print }' input.txt Input chr1 955543 955763 AGRN-6|pr=2|gc=75 0 + chr1 957571 957852 AGRN-7|pr=3|gc=61.2 0 + chr1 970621 ... (7 Replies)
Discussion started by: cmccabe
7 Replies

6. Shell Programming and Scripting

awk repeat one field at all lines and modify field repetitions

Hello experts I have a file with paragraphs begining with a keeping date and ending with "END": 20120301 num num John num num A keepnum1 num num kathrin num num A keepnum1 num num kathrin num num B keepnum2 num num Pete num num A keepnum1 num num Jacob num... (2 Replies)
Discussion started by: phaethon
2 Replies

7. Shell Programming and Scripting

Field Substitution

Hallo Team, I have more than 2000 rows of data. The file is in a CSV format. Field 24 on my file is empty and i would like to populate field 24 with 10.10.11.31. Below is how one row looks like: ... (3 Replies)
Discussion started by: kekanap
3 Replies

8. Shell Programming and Scripting

AWK: Pattern match between 2 files, then compare a field in file1 as > or < field in file2

First, thanks for the help in previous posts... couldn't have gotten where I am now without it! So here is what I have, I use AWK to match $1 and $2 as 1 string in file1 to $1 and $2 as 1 string in file2. Now I'm wondering if I can extend this AWK command to incorporate the following: If $1... (4 Replies)
Discussion started by: right_coaster
4 Replies

9. Shell Programming and Scripting

Awk Search text string in field, not all in field.

Hello, I am using awk to match text in a tab separated field and am able to do so when matching the exact word. My problem is that I would like to match any sequence of text in the tab-separated field without having to match it all. Any help will be appreciated. Please see the code below. awk... (3 Replies)
Discussion started by: rocket_dog
3 Replies

10. Shell Programming and Scripting

awk, comma as field separator and text inside double quotes as a field.

Hi, all I need to get fields in a line that are separated by commas, some of the fields are enclosed with double quotes, and they are supposed to be treated as a single field even if there are commas inside the quotes. sample input: for this line, 5 fields are supposed to be extracted, they... (8 Replies)
Discussion started by: kevintse
8 Replies
Login or Register to Ask a Question