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
# 1  
Old 08-21-2014
Replacing last line with awk and change the file name

Hi Guys,

I am having a set of date format files files where I am performing the below set of operations in the files . I Need to replace the last line value with specific date which is a pipe delimited file.

for eg
Code:
f1_20140101.txt
 
aa|aus|20140101|yy
bb|nz|20140101|yy
.
.
3|20140101|zz
 
f2_20140101.txt
 
aa|aus|20140101|yy
bb|nz|20140101|yy
.
.
3|20140101|zz

I need to replace the file names f1_20140101.txt,f2_20140101.txt to f1_20140303.txt, f2_20140303.txt and to replace the last line like below
Code:
f1_20140303.txt
 
aa|aus|20140303|yy
bb|nz|20140303|yy
.
.
3|20140303|zz
 
f2_20140303.txt 
aa|aus|20140303|yy
bb|nz|20140303|yy
.
.
3|20140303|zz

the changed value columns are fixed postition

Last edited by rbatte1; 08-21-2014 at 12:15 PM..
# 2  
Old 08-21-2014
Dear rohit_shinez,

I have a few to questions pose in response first:-
  • What have you tried so far?
  • What output/errors do you get?
  • What OS and version are you using?
  • What are your preferred tools? (C, shell, perl, awk, etc.)
  • What logical process have you considered? (to help steer us to follow what you are trying to achieve)
Most importantly, What have you tried so far? There are several threads on this sort of thing already, but it depends what OS & version you are using as to whether the features exploited are available.

There are probably many ways to achieve most tasks, so giving us an idea of your style and thoughts will help us guide you to an answer most suitable to you so you can adjust it to suit your needs in future.


We're all here to learn and getting the relevant information will help us all.



Robin
# 3  
Old 08-23-2014
Hi,

I am using Sun Solaris OS and its a shell script
so far i have tried with below code
Code:
for i in f1*.txt
do
awk 'BEGIN {OFS=FS="|"}$3==20140101{$3=20140303}{print}' $i>$1temp
mv $itemp `basename "$i" temp`.20140303
done

But not able to replace the last line of my file and the file name is not getting replaced based on the below output i required
Code:
f1_20140303.txt
 
aa|aus|20140303|yy
bb|nz|20140303|yy
.
.
3|20140303|zz
 
f2_20140303.txt 
aa|aus|20140303|yy
bb|nz|20140303|yy
.
.
3|20140303|zz


Last edited by Don Cragun; 08-23-2014 at 02:55 PM.. Reason: Fix bold tags
# 4  
Old 08-23-2014
Your desired output is a little ambiguous. Are the filenames included as text on the first line in the files?

Is the number to be replaced in the files a constant, or should it be extracted from the name of the file being processed.

Your for loop is only looking for files starting with f1, but it looks like you want to process all files starting with f and ending with .txt or maybe starting with f followed by one (or one or more) digits followed by an underscore and any string of 8 digits (or a particular string of 8 digits) followed by and ending with .txt. Please describe in English which set of files should be processed.

If more than one date's input files are to be processed as a set and the output files are supposed to have a single output date, are the numbers between the f and the _ in the output filenames supposed to be adjusted? If so, does the output order matter? Will there ever be more than 9 input or output files for a given date?
# 5  
Old 08-23-2014
Apart from what Don Cragun says, two comments on your script:
- as you want to rename the files anyhow, no temp file is needed (whatever $1temp may be, and the new file name will not be what you specified in post#1).
- the last line is not modified as the date string is in field 2 there, not in field 3.
# 6  
Old 08-23-2014
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.
2. Need to replace the file naming standard to specific date for e.g. f1_20140101.txt to f1_20140303.txt
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

# 7  
Old 08-23-2014
You might want to try this straightforward solution, which would satisfy your requirements with your sample data, but might fail with other file structures that you did not mention yet:
Code:
OD="20140101"; ND="20140303"
awk -v OD="$OD" -v ND="$ND" '{sub (OD, ND, FILENAME); sub (OD, ND); print > FILENAME}' f*.txt

ls -1 f*.txt
f1_20140101.txt
f1_20140303.txt
f2_20140101.txt
f2_20140303.txt

cat f1_20140303.txt 
aa|aus|20140303|yy
bb|nz|20140303|yy
.
.
3|20140303|zz

This User Gave Thanks to RudiC 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