Replacing last line with awk and change the file name


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replacing last line with awk and change the file name
# 15  
Old 08-24-2014
Quote:
Originally Posted by rohit_shinez
Hi Don,

Please find my answers for your questions:

1.Yes the date will be appeared in any field but i want the date to be replaced in 3rd position alone if it matches old date
2.No the date will not appear anywhere for the last line other than 2nd position. This pattern is only for last line
Please answer ALL of my questions. Trying to write and rewrite code for you from incomplete specifications is a waste of all of our time!
# 16  
Old 08-24-2014
Quote:
Originally Posted by Don Cragun
So the f2_20140101.txt in messages #1 and #3 in this thread were just there to confuse us???

RudiC's suggested code ignores your answer here and modifies all matching files shown in the samples in those earlier posts.

Your loop was looking for f1*.txt which implies that there could be a f1_20140101.txt, f1_2040205.txt, and a lot of other files that match the pattern. Will there only be one file that matches that pattern? Or, is your intent to remove all but one of the matching files, and then modify and move the last matching file to use the new date (which is what your script seems to do)?

RudiC's code assumes only one source date exists (even though it doesn't remove the old files after the conversion to the new date).

Will the date to be changed ever appear in any field other than field 3 in any line other than on the last line in the file? Will the date to be changed ever appear in any field other than field 2 in the last line in the file? RudiC's code assumes the answer to both of these is no. (And that is a reasonable assumption given the sample data you've provided. If his assumption is incorrect, you can use different code to handle your data. But we can make simplifying assumptions if you confirm that the date to be changed can never appear in field 1 and only appears in field 2 on the last line of your input files.)

You have said this is to be a shell script. On SunOS systems there are huge differences between sh and ksh, and some minor differences between ksh and bash. Can we use bash or ksh (or /usr/xpg4/bin/sh)?

RudiC's code asks you to fill in the old date manually. If you do that and there is only one old date to process, the shell doesn't matter (as long as it isn't csh or a csh derivative. If you want the script to extract the old date from the name of the file being processed, /bin/sh requires a slower and (at least to some) more complicated method to extract the date from the name than newer shells.
Hi Don,

Please find my answers sorry to bother you again and clear on my requirement:
  1. Since I am using f1*.txt there will be only files with same dated format pattern like f1_20130101.txt, f2_20130101.txt, f3_20130101.txt
  2. As per RudiC, it shouldn't move the older files which is what I also need just to redirect to another file with new dated format
  3. Coming on to replacing the date in file:
    1. Yes the date will be appeared in any field but I want the date to be replaced in 3rd position alone if it matches old date
    2. No the date will not appear anywhere for the last line other than 2nd position. This pattern is only for last line
  4. The script can be written in ksh /bash
  5. Old date can be made manually as per RudiC's code which will fit my requirement

Apart from this as I said earlier RudiC's code worked as per requirement only but problem is since sub is being used it will replace the first occurrence but as I mentioned in point a

Last edited by rbatte1; 08-27-2014 at 10:26 AM.. Reason: Set list as numbered LIST tags. Capitalise first person singular
# 17  
Old 08-24-2014
So - why don't you adapt the proposal to fit to your needs? You had an awk statement already in one of your early posts, so you should be able to join the two together to get at the perfect solution.
# 18  
Old 08-24-2014
Looking back at the other 22 threads you have started and the suggestions that have been given to you, I agree with RudiC that you should have everything you need to solve this problem (although doing all of it in a single awk script is a little trickier than most of the requests in your other threads).

Please try adapting RudiC's suggestion to meet your needs and then let us see how close you can come to making it work.

And, in your last response you said "1.Since i am using f1*.txt , there will be only files with same dated format pattern like f1_20130101.txt,f2_20130101.txt,f3_20130101.txt". Which makes no sense to me. If you're using the pattern f1*.txt, that will match f1_20130101.txt, f1_20140101.txt and f1_20140303.txt (all of which have been listed as input or output files in this thread). It will never match any filename starting with f2_ or f3_. RudiC's code used the pattern f*.txt which will match every number before the underscore and every date. I still don't understand which files you want to change! (I would have guessed you wanted to process all files that match the pattern f*_"$OD".txt, but that is not supported by any of your stated requirements nor by the code you supplied.)
# 19  
Old 08-24-2014
Hi ,

Yes Don the file should process with f*_"$OD".txt

Tried with the below one but getting an error stating illegal statement
Code:
OD="20140101"; ND="20140303"
awk -v OD="$OD" -v ND="$ND" '{sub (OD, ND, FILENAME);$3==OD{$3=ND};print >FILENAME}' f*.txt

# 20  
Old 08-24-2014
Quote:
Originally Posted by rohit_shinez
Hi ,

Yes Don the file should process with f*_"$OD".txt

Tried with the below one but getting an error stating illegal statement
Code:
OD="20140101"; ND="20140303"
awk -v OD="$OD" -v ND="$ND" '{sub (OD, ND, FILENAME);$3==OD{$3=ND};print >FILENAME}' f*.txt

I am very disappointed in your attempt. You say your script should process f*_"$OD".txt, but your script processes f*.txt. You say your script should update field 3 in some lines and field 2 in the last line. Your script makes no attempt to distinguish between the last line and any other line, and never looks at nor update field 2.

Try something like this instead:
Code:
#!/bin/ksh
IAm=${0##*/}
if [ $# -ne 2 ]
then	printf 'Usage: %s old_date new_date\n' "$IAm" >&2
	printf 'where old_date and new_date are in the format YYYYMMDD\n' >&2
	exit 1
fi
awk -v OD="$1" -v ND="${2}" '
BEGIN {	FS = OFS = "|"
}
NR > 1 {if(FNR == 1) 
		print uf2 > newfile
	else	print uf3 > newfile
}
FNR == 1 {
	if(NR > 1)
		close(newfile)
	newfile = FILENAME
	sub(OD, ND, newfile)
}
{	if($2 == OD) {
		save = $0
		$2 = ND
		uf2 = $0
		$0 = save
	} else	uf2 = $0
	if($3 == OD)
		$3 = ND
	uf3 = $0
}
END {	print uf2 > newfile
}' f*_"$1.txt"

The awk script could be simplified considerably if your shell script would determine the number of lines in each input file before invoking awk, but this more complex script gets rid of the need to have ICODE]awk[/ICODE] read each file twice or to have the shell or wc read the file to determine the line number of the last line in each file.
This User Gave Thanks to Don Cragun For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk sed to repeat every character on same position from the upper line replacing whitespace

Hello is it possible with awk or sed to replace any white space with the previous line characters in the same position? I am asking this because the file I have doesn't always follow a pattern. For example the file I have is the result of a command to obtain windows ACLs: icacls C:\ /t... (5 Replies)
Discussion started by: nakaedu
5 Replies

2. UNIX for Dummies Questions & Answers

awk command not replacing in first line

As per requirement if column 2 is NULL then 'N' ELSE 'Y'. I have written below awk code. But it is not replacing values for first line. :confused: cat temp.txt 1|abc|3 1||4 1|11|c awk -F'|' '{if($2==""){$2="N"}else{$2="Y"} print $0 } {OFS="|"} ' < temp.txt 1 Y 3 ... (4 Replies)
Discussion started by: max_hammer
4 Replies

3. Shell Programming and Scripting

Replacing multiple line patterns with awk

Hi forum, Can you please help me understand how to look for and replace the below pattern (containing line breaks) and return a new result? Rules: Must match the 3 line pattern and return a 1 line result. I have found solutions with sed, but it seems that sed installed in my system is... (5 Replies)
Discussion started by: demmel
5 Replies

4. Shell Programming and Scripting

Replacing lines matching a multi-line pattern (sed/perl/awk)

Dear Unix Forums, I am hoping you can help me with a pattern matching problem. What am I trying to do? I want to replace multiple lines of a text file (that match a multi-line pattern) with a single line of text. These patterns can span several lines and do not always have the same number of... (10 Replies)
Discussion started by: thefang
10 Replies

5. Shell Programming and Scripting

awk for replacing line feed

Hello all, I have data like "1"|"My_name"|"My_Email"|"My_Last"|My_other" "2"|"My_name"|"My_Email"|"My_Last"|My_other" "3"|"My_name"|"My_Email"|" "|My_other" "1"|"My_name"|"My_Email"|"My_Last"|My_other" Need output like "1"|"My_name"|"My_Email"|"My_Last"|My_other"... (10 Replies)
Discussion started by: lokaish23
10 Replies

6. Shell Programming and Scripting

Replacing a line in a file

Hi all, I need to replace a line export TZ=xxxxxxxx with the line export TZ=$1 Now, "xxxxxxxx" in the above line is some unknown string and $1 is a parameter. I want the content of $1 to be replaced with "xxxxxxxx". Kindly help me how to do this in the shell scripting. (5 Replies)
Discussion started by: ddeeps2610
5 Replies

7. Shell Programming and Scripting

Replacing a line in a file using sed

I have a file which has a list in it pop triangle people slow fast What I want to do is search this list and replace people with humans do the list looks like this: pop triangle human slow fast I think i use something like this.... if cat /list.txt | grep -q 'people' ; then (9 Replies)
Discussion started by: digitalviking
9 Replies

8. Shell Programming and Scripting

Replacing line 'i' of file1 with line 'j' of file 2

Hi All, As mentioned in the title I have two text files and I would like to replace line number 5 of file #1 with line number 4 of file #2 e.g. file 1 wqwert 4.4464002 3 319 286 369 46.320002 56.150002 45.100002 1 1 1 0.723 (12 Replies)
Discussion started by: f_o_555
12 Replies

9. Shell Programming and Scripting

awk one line, change \n to sth in a file

Hi Everyone, cat 1.txt aaa bbb ccc outout will be cat 2.txt ,,aaa,,bbb,ccc,, means change "\n" to ",,", and add ",," into the beginging and ending. right now i am using perl while to open and read the file, then split \t, feel not nice. please advice. and i hear using perl... (8 Replies)
Discussion started by: jimmy_y
8 Replies

10. Shell Programming and Scripting

Replacing a line in a file - HELP!!!

I have a problem in the following code ... while read line do #Get Line Number OLDLINE=`sed -n $Lineno $filename` echo "Un Changed Line : "$OLDLINE echo "Enter a New Pattern : " read NewPattern <&1 echo "NewPattern :"$NewPattern NEWLINE=`cat $filename | sed -n... (1 Reply)
Discussion started by: maxmave
1 Replies
Login or Register to Ask a Question