Hello Guys, How all are doing?
I have an issue in Unix and want help from all of you
I have a file in UNIX which it read by line by line , If at the end of line '0' is written the it should fetch that line into another file and change '0' to '1'
and If at the end of line '1' is written then it should do nothing.
Temp file cannot be used
Input and output samples please.
Why? Homework?
NO, Its not the homework, We never use the temp files in our Coding Standard. AND Rule is Rule..
pamu
December 11, 2012, 9:12am
4
Is this what you want..?
$ cat file
rel/prod/amp.cfg1
rel/fld/amp.cfg0
rel/fld/amp.cfg/sdd1
rel/fld/amp.cfg/doc/cam/amp.cfg0
$ awk '{if(substr($0,length($0),1) == 0){print > "new_file";sub("0$","1")}}1' file
rel/prod/amp.cfg1
rel/fld/amp.cfg1
rel/fld/amp.cfg/sdd1
rel/fld/amp.cfg/doc/cam/amp.cfg1
$ cat new_file
rel/fld/amp.cfg0
rel/fld/amp.cfg/doc/cam/amp.cfg0
To change original file with sed..
try
sed -i 's/0$/1/' file
cero
December 11, 2012, 9:31am
5
The sed solution that ignores lines ending with 1 would look like this:
sed -e '/1$/d;s/0$/1/' /path/to/input >/path/to/output
PS: sed -i creates a tempfile somewhere...
pamu
December 11, 2012, 9:35am
6
cero:
The sed solution that ignores lines ending with 1 would look like this:
sed -e '/1$/d;s/0$/1/' /path/to/input >/path/to/output
PS: sed -i creates a tempfile somewhere...
As per OP's condition you should use..
sed -i '/1$/d;s/0$/1/' file
But that file is not part of the code..
No, It should change the old file form '0' to '1' and fetch the record into another file which has '0' marked in the master file
pamu
December 11, 2012, 9:42am
8
Check this out..
$ cat file
rel/prod/amp.cfg1
rel/fld/amp.cfg0
rel/fld/amp.cfg/sdd1
rel/fld/amp.cfg/doc/cam/amp.cfg0
$ awk '{if(substr($0,length($0),1) == 0){sub("0$","1");print > "new_file"}}' file
$ cat new_file
rel/fld/amp.cfg1
rel/fld/amp.cfg/doc/cam/amp.cfg1
Really Sorry, The original case is-
A file containing three field f1-f2-f3
These fields are separated by '-'
while IFS='-' read -r f1 f2 f3
do
if [ $f3 != '0' ]
then
EXECUTE SOME FUNCTION
fi
#After Processing It should change f3 to 1
done < ${DATE_FILE}
Example Suppose the file is
1-Process-0
2-Task-1
3-Server-0
So when I execute the scripts it should do as
1-Process-1
2-Task-1
3-Server-1
That would violate their coding standard of never using a temp file, because -i uses a temp file. It's not magic.
That said, 'never use a temp file' is a pretty arbitrary and pointless coding standard for UNIX shell scripting.
Any Idea Guys.. I tried lot..