Maybe the following will help you get what you need. It was written and tested using a Korn shell, but will work with any shell that understands basic POSIX required parameter expansions.
If you invoke this script with not operands, it will try to update a file named SAMPLE.txt. If you invoke it with an operand, it will use the 1st operand as the pathname of the file to be updated.
Code:
#!/bin/ksh
# Get name of file to be processed:
INFILE=${1:-SAMPLE.txt}
OUTFILE="$INFILE.new"
# Convert the input file to a DOS formatted text file by completing the last
# input line (in case the last input line is incomplete..
printf '\r\n' >> "$INFILE"
# Use awk to search for and, if found, replace strings of asterisks in two
# locations on each line to strings of zeroes, and log any changes made.
# Delete blank lines in case the last input line was complete and we added an
# extraneous, empty, DOS format line.
awk -v OUTFILE="$OUTFILE" '
BEGIN { logfmt = "Fix applied: Line:%d, Offset:%d, Customer:%s\n"
spot[++ns] = 1050
spot[++ns] = 1249
}
/^[[:blank:]]*\r*$/ {
# Skip blank lines (with DOS or UNIX line terminators).
next
}
{ # Uncomment next line to change line terminators from DOS to UNIX.
# sub(/\r$/, "")
# Search the specified spots in each input line for 10 asterisks...
for(i = 1; i <= ns; i++)
if(substr($0, spot[i], 10) == "**********") {
# and when found, change them to zeros...
$0 = substr($0, 1, spot[i] - 1) "0000000000" \
substr($0, spot[i] + 10)
# and log the cange made.
printf(logfmt, NR, spot[i], substr($0, 1, 10))
}
# Copy the (possibly updated) input line to the output file.
print > OUTFILE
}' "$INFILE" && cp "$OUTFILE" "$INFILE" && rm -f "$OUTFILE"
# If the conversion succeeded, the above line replaces the contents of the
# input file (to avoid breaking any links to the input file), and if the copy
# succeeds removes the temp file holding the updated input.
If someone else wants to try this on a Solaris/SunOS system, change awk in the script to /usr/xpg4/bin/awk (nawk won't work for this script).
With the SAMPLE.txt file you provided as an input file, the awk output is:
and the spots indicated above in SAMPLE.txt are changed from ********** to 0000000000, and a DOS <CR><NL> is added to the end of SAMPLE.txt to complete the incomplete line.
And, if you uncomment the line shown in red in the script, it will convert DOS format lines in the input file into UNIX format lines.
I am not sure how to run that. I tried just copy & past into Putty but it does not seem to work.
[
Code:
#!/bin/ksh
# Get name of file to be processed:
INFILE=${1:-/home/download/east/master.dat}
OUTFILE="/home/download/east/$INFILE.new"
# Convert the input file to a DOS formatted text file by completing the last
# input line (in case the last input line is incomplete..
printf '\r\n' >> "$INFILE"
# Use awk to search for and, if found, replace strings of asterisks in two
# locations on each line to strings of zeroes, and log any changes made.
# Delete blank lines in case the last input line was complete and we added an
# extraneous, empty, DOS format line.
awk -v OUTFILE="$OUTFILE" '
BEGIN { logfmt = "Fix applied: Line:%d, Offset:%d, Customer:%s\n"
spot[++ns] = 1050
spot[++ns] = 1249
}
/ub(/\r$/, "")
# Search the specified spots in each input line for 10 asterisks...
for(i = 1; i <= ns; i++)
if(substr($0, spot[i], 10) == "**********") {
# and when found, change them to zeros...
$0 = substr($0, 1, spot[i] - 1) "0000000000" \
substr($0, spot[i] + 10)
# and log the cange made.
printf(logfmt, NR, spot[i], substr($0, 1, 10))
}
# Copy the (possibly updated) input line to the output file.
print > OUTFILE
}' "$INFILE" && cp "$OUTFILE" "$INFILE" && rm -f "$OUTFILE"^[[:blank:]]*\r*$/ {
# Skip blank lines (with DOS or UNIX line terminators).
next
}
{ # Uncomment next line to change line terminators from DOS to UNIX.
sub(/\r$/, "")
# If the conversion succeeded, the above line replaces the contents of the
# input file (to avoid breaking any links to the input file), and if the copy
# succeeds removes the temp file holding the updated input.]
You run it just like you run any other shell script:
You copy the text of the script into a regular file with a name that you choose (let us use the name my_script for this example) using an editor that uses the <newline> character as a line terminator (not the DOS <carriage-return><newline> character pair line terminator).
You execute the command: chmod +x my_script to make your script executable.
And then you run your script.
How you run your script depends on what directory you are in, what directory my_script is in, and what file you want your script to process.
The part of the line in the script:
Code:
INFILE=${1:-/home/download/east/master.dat}
shown in red names the file that will be processed when you run this script if you do not specify an operand. If you want to process a file other than this default, you will specify the name of the file you want to process as the command-line argument when you invoke your script. If the file you want to process is in the directory you are sitting in when you run your script, the argument can just be the last component of the file's name; otherwise you will have to supply an argument that is an absolute or relative pathname (relative to the directory you are in when you run the script).
If you are in the directory where your script is located, you can invoke it with:
Code:
./my_script
to run it to process the default file, or with:
Code:
./my_script /path/of/file
to process a file with the given pathname.
If your script is located in a directory that is on the command search path specified by your PATH environment variable, you can invoke it with just:
Code:
my_code
or:
Code:
my_code pathname
And, if your script is not in the current directory and is not on your search path, you can invoke it using the absolute pathname of your script or a pathname of script relative to the directory in which you are sitting:
That worked! I was able to call it from my main script & write to the log file for the main script. I was able to send it in an email also!
Thank you!
I'm glad it worked for you.
Note that if you find a post particularly helpful in achieving your goal or helping you to understand how to get your shell or operating system to do what you want, you can hit the Thanks button at the lower left corner of that post to express your thanks to the person who submitted it.
Hi.
I need to append/prefix an & character to every 'single' & character (not when there are 2 or more grouped together) I find in a file. I can do it using this cmd:
cat ${file} | sed -e 's/&/&&/g' > ${new_file}
How can I modify this to ensure I only replace single &'s and not operate... (11 Replies)
The content of the file filea.txt is as follows.
---------
case $HOSTNAME in
aaa)
DS_PARM_VALUE_SET=vsDev
APT_Configuration_File=/appl/infoserver/Server/Configurations/2node.apt
;;
bbb)
DS_PARM_VALUE_SET=vsQA... (3 Replies)
Hello All,
I need a sed command to find and replace below text in multiple files in a directory.
Original Text :- "$SCRIPT_PATH/files"
Replace with :- "$RESOURCE_FILE"
Thank you in advance !!!
Regards,
Anand Shah (1 Reply)
Hi All,
I have a file having one line only.
It is like
trapsess:inform|10.232.167.18|1|1|50|25|0|0|0|5|1|1|78|0037|
I want to replace the numbers in last two columns by As.
It should look like
trapsess:inform|10.232.167.18|1|1|50|25|0|0|0|5|1|1|AA|AAAA|
Please, suggest me any shell... (12 Replies)
Hello,
I have this command and it works fine.
My question is that how can we replace the N by a variable, to print for instance a big number of lines. It means if I want 100 lines after an expression, to not put "N" 100 times in the sed.
Code:
$ sed -n '/aaa/{n;N;N;s///g;s/;/; /g;p;}'... (2 Replies)
Okay, title is kind of confusion, but basically, I have a lot of scripts on a server that I need to replace a ps command, however, the new ps command I'm trying to replace the current one with pipes to sed at one point. So now I am attempting to create another script that replaces that line.
... (1 Reply)
Hi
I am very new to linux and scripting.
I need to replace numbers abc with number xyz inputting from a reference file.
I used the following command -
sed "s/$grd/$lab/" , where $grd and $lab comes from reference file.
The problem is the above line doesnt take care of space..... (1 Reply)
Hi,
I would like to seek help on how i can arrive on this result.
sample.txt:
product_code IN (param001) and product_type IN (param004)
product_code IN (param002) and product_type IN (param005)
product_code IN (param003) and product_type IN (param006)
I would like to change the param001... (1 Reply)