change the format of a giving file ( a bit challenge) Thank you


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting change the format of a giving file ( a bit challenge) Thank you
# 1  
Old 12-03-2007
change the format of a giving file ( a bit challenge) Thank you

Hi,

I have a file like this:

mgr1.dbf
tool.dbf
usr.dbf
wow19.dbf
wow2wow.dbf

Can anyone help change the format after each line of reading to:

mgr2.dbf # add 1 of *1.dbf
tool2.dbf # if not a number exist, make it default to 2
usr2.dbf
wow20.dbf # add 1 of *19.dbf
wow3wow.dbf # add 1 of *1*.dbf

thank you for the help!
# 2  
Old 12-03-2007
That's a pretty interesting one!
perl looks like the most appropriate choice for this:
Code:
#!/usr/bin/perl -w
while (<>) {
  if (/^([^\d\.]*)(\d*)([^\d.]*)\.([^\.]+)$/) {
    if ($1) { print "$1" }
    if ($2) {
      $val=$2+1;
      print $val;
      $suffix="";
    } else {
      $suffix="2";
    }
    if ($3) { print "$3" }
    if ($suffix) { print "$suffix" }
    print ".$4";
  } else {
    print "# Could not parse the following line:\n";
    print "$_";
  }
}

# 3  
Old 12-04-2007
Thanks Smiling Dragon

It works great!


but is there a way that I can use korn shell to accomplish it?
# 4  
Old 12-04-2007
Computer

Quote:
Originally Posted by Smiling Dragon
That's a pretty interesting one!
perl looks like the most appropriate choice for this:
Code:
#!/usr/bin/perl -w
while (<>) {
  if (/^([^\d\.]*)(\d*)([^\d.]*)\.([^\.]+)$/) {
    if ($1) { print "$1" }
    if ($2) {
      $val=$2+1;
      print $val;
      $suffix="";
    } else {
      $suffix="2";
    }
    if ($3) { print "$3" }
    if ($suffix) { print "$suffix" }
    print ".$4";
  } else {
    print "# Could not parse the following line:\n";
    print "$_";
  }
}



here is a test result:

stzhaoSmiliets/7:sambar:dman > cutstring.pl test.txt
12rgm.log
mgr2.dbf
tool2.dbf
usr2.dbf
wow20.dbf
wow3wow.dbf
# Could not parse the following line:
11number12.dbf
# Could not parse the following line:
char14between13.dbf
# Could not parse the following line:

# Could not parse the following line:


Sorry, adding more test condition, if two or more numbers appearing in the string, can we increase the last number?

like:
11number12.dbf --> 11number13.dbf
char14between13.dbf --> char14between14.dbf
# 5  
Old 12-04-2007
Well, I appreciate the urge to "just do it" in ksh - so here goes:

Input:
Quote:
$ cat in.txt
mgr1.dbf
tool.dbf
usR.dbf
wow19.dbf
wow2wow.dbf
char14between13.dbf
11num@ber12.dbf
Script:
Quote:
#!/usr/bin/ksh

infile=in.txt
cat $infile | while read fname ; do
newname=$(echo "${fname}" |tr "[A-Z]" "[a-z]" |tr "[a-z]#_@-" "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" |tr -s "x")
num=$(echo $newname |cut -d"." -f1|awk -F"x" '{print $NF}')
if [ -z "${num}" ] ; then
num=1
newnum=$((num+1))
final=$(echo $fname|sed -e "s/\./$newnum\./g")
else
newnum=$((num+1))
final=$(echo $fname|sed -e "s/$num\./$newnum\./g")
fi
echo "$fname -> $final"
done
Result:
Quote:
$ inc.sh
mgr1.dbf -> mgr2.dbf
tool.dbf -> tool2.dbf
usR.dbf -> usR2.dbf
wow19.dbf -> wow20.dbf
wow2wow.dbf -> wow2wow2.dbf
char14between13.dbf -> char14between14.dbf
11num@ber12.dbf -> 11num@ber13.dbf
Obviously this is quite brittle but for the kind of input provided, this should do.

Note: done using default ksh on SunOS db012a 5.8 Generic_117350-35 sun4us sparc FJSV,GPUZC-M

HTH
# 6  
Old 12-04-2007
Bug

Quote:
Originally Posted by rikxik
Well, I appreciate the urge to "just do it" in ksh - so here goes:

Input:


Script:


Result:


Obviously this is quite brittle but for the kind of input provided, this should do.

Note: done using default ksh on SunOS db012a 5.8 Generic_117350-35 sun4us sparc FJSV,GPUZC-M

HTH

it works for most of creteria so I would say it is good enough, I will add some more features to it as well Smilie

Thank you all for the help!

Last edited by netbanker; 12-04-2007 at 02:55 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Change the file format

gpio_rw = gpiochip162 audio_aplay = HDMI_0 audio_aplay = HDMI_1 audio_aplay = HDMI_2 graphic_xrandr_show = USB1 graphic_xrandr_show = USB2 graphic_xrandr_show = USB3 graphic_change_resolution = eDP1 gpio_rw = "gpiochip162" audio_aplay = "HDMI_0 HDMI_1 HDMI_2" graphic_xrandr_show =... (3 Replies)
Discussion started by: yanglei_fage
3 Replies

2. Shell Programming and Scripting

Challenge to change file names

Hi, How can I change following file name in a bash script? From file names: myfile-module-1.0-3.0.el6.x86_64.package To file names: myfile-module1_0-1.0-3.0.el6.x86_64.package ^ ^ ^ ^ ^ ^ ^ ^ Basically, the digit 1.0 is a version number, the digit 3.0 is... (11 Replies)
Discussion started by: hce
11 Replies

3. Shell Programming and Scripting

howto change format of file?

Hi I have a file with this inside: How can I change it to: thanks a lot regards Israel. (3 Replies)
Discussion started by: iga3725
3 Replies

4. Shell Programming and Scripting

How to change date format in file

Hello! I have a textfile that look like this: "83d1:46:2b";"20091008190000";"Rögle BK - Skellefteå";"Swedish" "d4c:46:21";"20091008190000";"Södertälje - Brynäs";"Swedish" "d4b:46:2";"20091008190000";"HV 71 - Färjestad";"Swedish" "838:46:b";"20091010160000";"Skellefteå - HV 71";"Swedish"... (2 Replies)
Discussion started by: condmaster
2 Replies

5. Shell Programming and Scripting

switch format between 64 bit double and 64 bit integer

In order to perform some arithmetical operations on 64 bit double I need to transform of the binary representation of a 64 bit double to a 64 bit integer do the operations and then transform back to 64 bit double. I understand bash and bc doesn't allow to do that. I think my only tool left is... (2 Replies)
Discussion started by: Zephyr
2 Replies

6. Shell Programming and Scripting

To Change the file format Pls Help!!!

Hi All, I have a file like john::208:johnson john::208:mery john::208:test admin:*:1:johnson admin:*:1:test and wanna convert this as john::208:johnson,mery,test admin:*:1:johnson,test please help me to create a script for this thanks in advance John (4 Replies)
Discussion started by: johnsonpk
4 Replies

7. Shell Programming and Scripting

Giving format to a file

hi all, i have a big problem and i don't know what to do. This is the thing: i have a flat file with 26 fields, which are separated by ';' by example, i have this: Peter;Smith;2005;200508; ......... if the lengths of the fields are: field 1: Alphanumeric - field 2: Alphanumeric - field... (4 Replies)
Discussion started by: DebianJ
4 Replies

8. Shell Programming and Scripting

Change file format

Hi I have a file in the following format I have to convert this into four files , in the format as below. Data under Process SFA SUccess Section gets into file Named SFA_SUCCESS inthe following format ctr1,120 ctr2,1785 Data under Process SFA FAil gets into file Named SFA_Fail inthe... (2 Replies)
Discussion started by: sapics
2 Replies

9. HP-UX

Change 32 bit to 64 bit Kernel

Hi, I'm using HPUX 11.11 on Vizualize B180L our productive machines have 64 bit kernel # file vmunix vmunix: ELF-64 executable object file - PA-RISC 2.0 (LP64) I got one test system, which should have the same HW (i was told it is the same HW) hpuxtest:/stand-->file... (3 Replies)
Discussion started by: funksen
3 Replies

10. UNIX for Advanced & Expert Users

Change format to file II

hi all... i have a big problem, and i hope someone can help me... i have a flat file, and its columns are separated by comma (CSV), something like this: 78 , 204R180 , 90/100 , 200001 , 12 ,200002 ,13 ..... 78 , 204R180 , 90/100 , 200001 , 29 ,200002 ,30 ..... 78 , 204R180 , 90/100 ,... (0 Replies)
Discussion started by: DebianJ
0 Replies
Login or Register to Ask a Question