NOT elegant. For informational purposes only.
First, identify all of the
grep '^identifier1' my_file_name
This will print only the lines with your chosen tag. Later, you can look for records with a different identifier.
Once you have these records, you can search just column 14, with the cut command:
_command1_ | cut -c14
For clarity, I'm using shorthand notation on the commands. Hope they makes sense to you.
command2 uses the cut command to only print the 14th character (
-c for Character).
You can then look for 'Y' in this output.
_command2_ | grep -q Y The -q says "Quiet" -- don't output anything.
rc=$? Note: Some say you don't need rc, just look at $?. Your call.
At this point, if $rc (or $?) is 0, grep found at least one upper case Y. Otherwise, it will be 1 (other numbers are possible, indicating an error).
Let's code that this way:
if [ $rc -ne 0 ]
echo "No Ys found"
Note: if you place ANY commands between the
grep -q Y and the
if, you MUST use the
rc=$? Otherwise $? is fine.
To change ALL of the indicators for N to Y, you would run this sed command:
sed -i "/^identifier1/ s/\(………….\).\(.*\)/\1Y\2/" my_file_name
-i says In-place editing of the file.
/^identifier1/ says only match lines starting with indetifier1
s means search and replace. The SearchFor is after the / The Replace With is after the next slash.
SearchFor: You want the 14th character. There is an elegant way to do this in sed. I'll leave that for other folks to explain. The simple answer is:
-dot- means any character. So 13 dots means match the first 13 characters. Because we want to keep these in-tact, we'll store them in a bucket. The is it ( and ) -- escaped parenthesis. So:
/\( 13 dots \) . \( dot * \) say grab the first 13 characters of the line and store them in Bucket #1. the dot means match 1 character. the dot star says "Match 0 or more characters" -- i.e. everything else -- and store it in Bucket #2.
Now for the replace:
/\1Y\2/ says Replace the line with the contents of Bucket #1, followed by Y and then Bucket #2.
There is a way to clean this up and make it more efficient. I don't a Linux box in front of me and can't remember off the top of my head how to do it.
When some other FAB folks post it up, you ought to be able to understand it now.