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
# 8  
Old 08-23-2014
Hi,

i guess your snippet would replace where 20140101 exist in the file irrespective of field position in the file. The position of replacing the field column is fixed for me i.e. 3rd position in rest of the line of the file and 2nd position in last line of my file.
# 9  
Old 08-23-2014
Quote:
Originally Posted by rohit_shinez
Hi,

Let me be clear on the requirement

1. Files which ever starting from f and ending with .txt i.e. f*.txt. All the files naming standard will be in the format f1_YYYYMMDD.txt.
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.
Quote:
Originally Posted by rohit_shinez
2. Need to replace the file naming standard to specific date for e.g. f1_20140101.txt to f1_20140303.txt
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).
Quote:
Originally Posted by rohit_shinez
3.The step 2 condition will be done based on the replacing the dated column to specific date say here to 20130303. My input file i.e f1_20140101.txt will have data like this:
Code:
aa|aus|20140101|yy
bb|nz|20140101|yy
.
.
3|20140101|zz

the input file will follow the same pattern i need to replace the
1. 3rd dated position column to 20140303 in rest of lines
2. 2nd dated position column to 20140303 in last line of the input file

output some thing like this with file name replaced to f1_20140303.txt
Code:
aa|aus|20140303|yy
bb|nz|20140303|yy
.
.
3|20140303|zz

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.
# 10  
Old 08-23-2014
Hi Don,

Yes i can use bash and ksh.

The dated column on 3rd position of rest all line alone needs to be changed except the last line because in last line, the date appears in 2nd position which needs to be changed with new date.

The same date can appear in any other field position of the file but not be changed something like this

Code:
f1_20140303.txt

aa|aus|20140303|yy|20140101
bb|nz|20140303|yy|20140101
.
.
3|20140303|zz

# 11  
Old 08-23-2014
Quote:
Originally Posted by rohit_shinez
Hi Don,

Yes i can use bash and ksh.

The dated column on 3rd position of rest all line alone needs to be changed except the last line because in last line, the date appears in 2nd position which needs to be changed with new date.

The same date can appear in any other field position of the file but not be changed something like this

Code:
f1_20140303.txt

aa|aus|20140303|yy|20140101
bb|nz|20140303|yy|20140101
.
.
3|20140303|zz

You didn't answer any of my other questions and you didn't say RudiC's code is failing. So I assume his code is working perfectly for you.

If not, please answer my earlier questions.
# 12  
Old 08-24-2014
Hi Don,

Yes the code given by RudiC worked just wanted to know how the code checked the position to get replaced the exact position i.e. 3rd in rest and 2nd at last line irrespective of the dated column appeared in different location. I used the below file which worked perfectly
Code:
cat f1_20140101.txt
aa|aus|20140101|yy|20140101
bb|nz|20140101|yy|20140101
3|20140101|zz

Output achieved from the code which is what i required
Code:
cat f1_20140303.txt
aa|aus|20140303|yy|20140101
bb|nz|20140303|yy|20140101
3|20140303|zz

I believe that since sub will replace the first occurrence thats why its replacing but what it would be the case if the file is something like this

aa|20140101|20140101|yy|20140101
bb|nz|20140101|yy|20140101
3|20140101|zz

in the above case i need to replace only my 3rd position irrespective of any position i find the dated column.

Last edited by rohit_shinez; 08-24-2014 at 04:26 AM..
# 13  
Old 08-24-2014
Quote:
Originally Posted by rohit_shinez
Hi Don,

Yes the code given by RudiC worked just wanted to know how the code checked the position to get replaced the exact position i.e. 3rd in rest and 2nd at last line irrespective of the dated column appeared in different location. I used the below file which worked perfectly
Code:
cat f1_20140101.txt
aa|aus|20140101|yy|20140101
bb|nz|20140101|yy|20140101
3|20140101|zz

Output achieved from the code which is what i required
Code:
cat f1_20140303.txt
aa|aus|20140303|yy|20140101
bb|nz|20140303|yy|20140101
3|20140303|zz

I believe that since sub will replace the first occurrence thats why its replacing but what it would be the case if the file is something like this

aa|20140101|20140101|yy|20140101
bb|nz|20140101|yy|20140101
3|20140101|zz

in the above case i need to replace only my 3rd position irrespective of any position i find the dated column.
RudiC's code does not care what field is changed, it just changes the 1st occurrence of $OD it finds on a line (if there are any) to $ND. As I said, your sample data implies that the 2nd field in every line except the last line will be a lowercase alphabetic country code; not an eight digit number that looks like a date in YYYYMMDD or YYYYDDMM format. If your actual input file(s) contain a data field matching $OD before the field you wanted to change (as in your latest example), his code won't do what you want.

If RudiC's code is not sufficient for what you want, you need to answer my questions so we have a complete understanding of what this code is supposed to do.
# 14  
Old 08-24-2014
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
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