Need to log the sed command used to replace


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need to log the sed command used to replace
# 8  
Old 05-06-2016
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:
Code:
Fix applied: Line:13, Offset:1249, Customer:2y1023300 
Fix applied: Line:33, Offset:1050, Customer:2a4323413 
Fix applied: Line:34, Offset:1050, Customer:2a4323413 
Fix applied: Line:41, Offset:1050, Customer:2a5133020 
Fix applied: Line:45, Offset:1050, Customer:2a5203011 
Fix applied: Line:46, Offset:1050, Customer:2a5203011 
Fix applied: Line:49, Offset:1050, Customer:2a5231320

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.
# 9  
Old 05-10-2016
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.]

# 10  
Old 05-10-2016
You run it just like you run any other shell script:
  1. 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).
  2. You execute the command: chmod +x my_script to make your script executable.
  3. 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:
Code:
/path/to/my_script

or:
Code:
/path/to/my_script file

or:
Code:
/path/to/my_script /other/path/to/file

# 11  
Old 05-11-2016
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!

Last edited by tomj5141; 05-11-2016 at 01:18 PM..
# 12  
Old 05-11-2016
Quote:
Originally Posted by tomj5141
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 SmilieThanks button at the lower left corner of that post to express your thanks to the person who submitted it.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed Command to replace particular value.

Hi , My input file contain : list = 3 14 15 10 9 11 12 18 19 20 21 22 23 24 25 26 6 1 2 3 4 5 7 8 16 17 27 28 30 29 Expected output : list = 0 0 0 0 0 0 0 18 0 20 0 0 0 0 0 0 6 0 0 3 4 0 0 0 0 0 0 0 0 0 I want to keep the 8,10,16,17,22 value from the list and put 0 on rest of the... (9 Replies)
Discussion started by: Preeti Chandra
9 Replies

2. Shell Programming and Scripting

sed replace command

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)
Discussion started by: user052009
11 Replies

3. Shell Programming and Scripting

Find and replace using sed command

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)
Discussion started by: kmanivan82
3 Replies

4. Shell Programming and Scripting

sed command to find and replace

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)
Discussion started by: anand.shah
1 Replies

5. Shell Programming and Scripting

search and replace with sed command

hi, suggest me in the below script.. if In above I wanna replace "" with "]". (2 Replies)
Discussion started by: divya bandipotu
2 Replies

6. Shell Programming and Scripting

sed command to replace a character at last

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)
Discussion started by: mukeshbaranwal
12 Replies

7. Shell Programming and Scripting

Replace with a variable in sed command

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)
Discussion started by: rany1
2 Replies

8. Shell Programming and Scripting

Loop with sed command to replace line with sed command in it

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)
Discussion started by: cbo0485
1 Replies

9. UNIX for Dummies Questions & Answers

SED command to search for numeric and replace

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)
Discussion started by: ajayh
1 Replies

10. Shell Programming and Scripting

sed search and replace command

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)
Discussion started by: janzper
1 Replies
Login or Register to Ask a Question