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.
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:
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
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.
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
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
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.
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
Output achieved from the code which is what i required
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
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
Output achieved from the code which is what i required
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
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.
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
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)