File modification problem


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting File modification problem
# 8  
Old 02-15-2012
Quote:
Originally Posted by mnmonu
Hi ygemici,

Your code is correct. But when file contain 10000 row then problem. Then some loop is needed.
try this Smilie

Code:
# cat file1
"0001"~"name"~"bb"~"20.25"~"0010"
~"abc"~"0002"~"name"~"dd"~"35.50"~"25"~
"xxx"~"0003"~"name"~"aa"~"21.3

5"~"0056"~"ijk"~



"0001"~"name"~"bb"~"20.25"~"0010"
~"abc"~"0002"~"name"~"dd"~"35.50"~"25"~
"xxx"~"0003"~"name"~"aa"~"21.3

5"~"0056"~"ijk"~
"0001"~"name"~"bb"~"20.25"~"0010"
"0001"~"name"~"bb"~"20.25"~"0010"




~"abc"~"0002"~"name"~"dd"~"35.50"~"25"~
"xxx"~"0003"~"name"~"aa"~"21.3
5"~"0056"~"ijk"~
~"abc"~"0002"~"name"~"dd"~"35.50"~"25"~
"xxx"~"0003"~"name"~"aa"~"21.3

5"~"0056"~"ijk"~

Code:
# ./justdoit file1 "~" 6
"0001"~"name"~"bb"~"20.25"~"0010"~"abc"~
"0002"~"name"~"dd"~"35.50"~"25"~"xxx"~
"0003"~"name"~"aa"~"21.35"~"0056"~"ijk"~
"0001"~"name"~"bb"~"20.25"~"0010"~"abc"~
"0002"~"name"~"dd"~"35.50"~"25"~"xxx"~
"0003"~"name"~"aa"~"21.35"~"0056"~"ijk"~
"0001"~"name"~"bb"~"20.25"~"0010""0001"~"name"~
"bb"~"20.25"~"0010"~"abc"~"0002"~"name"~
"dd"~"35.50"~"25"~"xxx"~"0003"~"name"~
"aa"~"21.35"~"0056"~"ijk"~~"abc"~
"0002"~"name"~"dd"~"35.50"~"25"~"xxx"~
"0003"~"name"~"aa"~"21.35"~"0056"~"ijk"~

for save a file
Code:
# ./justdoit file1 "~" 6 >output

Code:
# cat justdoit
#!/bin/bash
## justdoit @ygemici unix.com simple parser ##
## Usage e.g -> ./justdoit FILENAME "SPLITCHAR" COUNT ##
backup() {
cp $1 $1.bck && cp $1.bck $1.sed
}
backup $1
if [ ! $? -eq 0 ] ; then
echo "There is a problem while backuping your FILE!!";exit 1
fi
doit() {
len=$(tr -cd "$2" <$1|wc -c)
sed ':e;N;s/\n//g;te' ${1}.sed >${1}.sed.bck && mv ${1}.sed.bck ${1}.sed
for ((i=$3;i<${len};i=i+$3));
do
sed 's/~/~X_XsedX/'$i'' ${1}.sed >${1}.sed.bck && mv ${1}.sed.bck ${1}.sed
done
sed 's/X_XsedX/\n/g' ${1}.sed
}
doit "$1" "$2" $3;rm -f ${1}.sed

regards,
ygemici
This User Gave Thanks to ygemici For This Post:
# 9  
Old 02-15-2012
Another one:
Code:
awk 1 RS=\~ infile | awk 'NF{if (/name/)print RS;print p;p=$0}END{print p}' | awk '{gsub(ORS,"~");print $0 "~"}' RS=

This User Gave Thanks to Scrutinizer For This Post:
# 10  
Old 02-16-2012
Quote:
Originally Posted by Scrutinizer
Another one:
Code:
awk 1 RS=\~ infile | awk 'NF{if (/name/)print RS;print p;p=$0}END{print p}' | awk '{gsub(ORS,"~");print $0 "~"}' RS=

Thanks Scrutinizer.

But little problem in your script. End of output file, print the line no. Just like
Quote:
"0001"~"name"~"bb"~"20.25"~"0010"~"abc"~
"0002"~"name"~"dd"~"35.50"~"25"~"xxx"~
"0003"~"name"~"aa"~"21.35"~"0056"~"ijk"~
.......................................
.......................................
.......................................
"0500"~"name"~"aa"~"21.35"~"0056"~"gdsj"~"0500"~
see last line last value print "0500", that is extra

Please help me

Last edited by mnmonu; 02-16-2012 at 05:11 AM..
# 11  
Old 02-16-2012
I can't reproduced this, could you tail your input sample and post it?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help For File modification

Hi, I have a file. File contains are as follows : Feb 19, 2012 5:05:00 PM org.apache.coyote.http11.Http11Protocol init INFO: Initializing Coyote HTTP/1.1 on http-8080 Feb 19, 2012 5:05:00 PM org.apache.catalina.startup.Catalina load INFO: Initialization processed in 771 ms Feb 20, 2012... (3 Replies)
Discussion started by: mnmonu
3 Replies

2. Shell Programming and Scripting

File Modification

Hi, I have a file input.txt. cat input.txt output is as follows : Code: "0001"~"name"~"bb"~"20.25"~""~""~"0002"~"name" "dd"~"35.50"~"" ~""~"0003"~"name"~"aa"~"21.3 5"~""~""~ I want the output looking like: cat output.txt Code: "0001"~"name"~"bb"~"20.25"~""~""~... (6 Replies)
Discussion started by: mnmonu
6 Replies

3. Shell Programming and Scripting

Help for File Modification

Hi All, I have a file. This file contain huge amount of data. I want to modify this file. I want enter new line when count of "~ character is 79. Please find below the code : cat file_name | tr -d '\n' | sed... (6 Replies)
Discussion started by: mnmonu
6 Replies

4. Shell Programming and Scripting

Help for File Modification

Hi All, I have a file disk_space.log. cat disk_space.log 94% / 32% /boot 38% /mnt/data 100% /media/CDROM I want the output, like cat disk_space.log 94% / 100% /media/CDROM That means print the line those are grater-than 90%. And rest of the line is remove from file. I have a... (2 Replies)
Discussion started by: mnmonu
2 Replies

5. Shell Programming and Scripting

Help for File Modification

Hi, I have a file abcd.txt. cat abcd.txt output is as follows : "aa"~"bb"~"001"~""~""~"cc" "dd"~"005"~"" ~""~"kk"~"aa"~"00 8"~""~""~ I want the output looking like: cat abcd.txt "aa"~"bb"~"001"~""~""~ "cc""dd"~"005"~""~""~ "kk"~"aa"~"008"~""~""~ I have a script. (4 Replies)
Discussion started by: mnmonu
4 Replies

6. Shell Programming and Scripting

Help with file modification

Hi, I have a file test.txt . The contain of the file is as below : 365798~SAPUS~PR5~0000799005~ADM CHARG MEDCAL INS~~~~~~~~~~~~~~~~~~~~~~~~SLAC480 I want to modify this file. And file contain loking like "365798"~"SAPUS"~"PR5"~"0000799005"~"ADM CHARG MEDCAL... (6 Replies)
Discussion started by: mnmonu
6 Replies

7. Shell Programming and Scripting

File modification history

Can anyone please suggest an alternate command for "stat" . I am trying this on Solaris 5.9 , but the command doesn't exist. Basically i need to see one particalar file modification history. Any help is appreciated. (4 Replies)
Discussion started by: mk1216
4 Replies

8. UNIX for Dummies Questions & Answers

How to change the file modification time of a file on nfs mount point

Hi I am accessing a file on nfs mounted device, after completing using of the file, i am tring to restore the access time and modification times of the file. So i got the previous modified time of the file using stat() function and trying to set the date and time for the file, To set these... (6 Replies)
Discussion started by: deepthi.s
6 Replies

9. Shell Programming and Scripting

File modification

Dear all, i have a file which contains this lines. 0-0 CC=1 0-01 0-011 0-0111 0-01110 F=500 CC=1 L=15 M=5 TRD=3948... (2 Replies)
Discussion started by: panknil
2 Replies

10. AIX

snmpd modification problem

Hi Gurus, I am relatively new in AIX. I have a problem in modifying the community string in snmpd. I have edited the /etc/snmpd.conf file and changed the default community name 'public' to new one (P@@$w0rd). When I tried to query my aix server with the new string , it is not giving any... (1 Reply)
Discussion started by: helloajith
1 Replies
Login or Register to Ask a Question