Specialty Forums
Security and Virus
General Hardware
CPUs/Overclocking
Networking
Digital Photo/Video
Office Software
PC Gaming
Console Gaming
Programming
Database
Web Development
Digital Home

General Forums
Windows XP
Windows Vista
Windows 95/98
Windows Me
Windows NT
Windows 2000
Win Server 2008
Win Server 2003
Windows 3.1
Linux
PDAs
BeOS
Novell Netware
OpenVMS
Solaris
Disk Op. System
Unix
Mac
OS/2

Drivers
Driver Scan
Driver Forum

Software
Automatic Updates

BIOS Updates

My Computing.Net

Solution Center

Free IT eBook

Howtos

Site Search

Message Find

RSS Feeds

Install Guides

Data Recovery

About

Home
Reply to Message Icon Go to Main Page Icon

Subject: copy and past in a file

Original Message
Name: Fouad
Date: April 15, 2008 at 09:22:33 Pacific
Subject: copy and past in a file
OS: AIX 5.2
CPU/Ram: 4 / 8GB
Model/Manufacturer: IBM
Comment:
Hi,
My query is as follows:
(1) I want to write a Schell Script which will first read files (file as variable).Each file contains multiple lines/records.
(2) Each line /record in a file has a multiple characters
(3) So I want a shell script - which will copy the CHARACTERS between the position 10 und 20 and past them in the position 40-50 of each line .

Thanks in advance


Report Offensive Message For Removal

Response Number 1
Name: nails
Date: April 16, 2008 at 07:02:56 Pacific
Subject: copy and past in a file
Reply: (edit)
One way is to cut each line into the proper lengths and then paste them back together.

You are not clear whether the characters between positions 10 and 20 should be eliminated so I left it in:


#!/bin/ksh

while read line
do
length=${#line}
if [[ $length > 50 ]]
then
f10="$(echo "$line" | cut -c10-20)"

f1="$(echo "$line" | cut -c1-40)"
f3="$(echo "$line" | cut -c50-$length)"
printf "%s%s%s\n" "$f1" "$f10" "$f3"
fi
done < datafile.txt



Report Offensive Follow Up For Removal

Response Number 2
Name: Fouad
Date: April 16, 2008 at 07:35:31 Pacific
Subject: copy and past in a file
Reply: (edit)
The file look ~ like that:
line1:uTN02PS01AGC AUTO00129325 001293251400584180 55PRV0201
line2: TN02PS01AGC AUTO00129326 001293261400584180 55PRV0201
line3: uTN02PS01AGC AUTO00129327 001293271400584180 55PRV0201
....and so on.

The caracters between positions 10 and 20 should be not eliminated but copied and pasted in the position 40-50 of each line.

I apreciate your help nails, I run the script you proposed but I do not see any difference between the original file and the new one(no change has been made).
Any other suggestion?

Thanks
Fouad


Report Offensive Follow Up For Removal

Response Number 3
Name: nails
Date: April 16, 2008 at 09:11:23 Pacific
Subject: copy and past in a file
Reply: (edit)
I took your first like and placed it in datafile.txt.

Here's what the script does:
if the length of the line is greater than 50 characters.

1) cut out characters 10 20
2) cut out characters 1 to 40
3) cut out characters 50 to the end of the line
4) glue together step 2), step 1), and step 3

The first line if yours and the second line is after the script runs. It looks correct to me:


uTN02PS01AGC AUTO00129325 001293251400584180 55PRV0201
uTN02PS01AGC AUTO00129325 00129325140058AGC AUTO001V0201


Report Offensive Follow Up For Removal

Response Number 4
Name: Fouad
Date: April 16, 2008 at 09:26:14 Pacific
Subject: copy and past in a file
Reply: (edit)
Hi Nails,

you 're right the only thing to change is step 3): here must the line be cut from caracters 40 to the end of it.
Now the script works fine.
BTW: how can I define the input file as variable?

Anyway many tanks for your help.

Fouad


Report Offensive Follow Up For Removal

Response Number 5
Name: nails
Date: April 16, 2008 at 10:52:24 Pacific
Subject: copy and past in a file
Reply: (edit)
This code checks if you passed the file name as a command line argument to your script. If no command line argument was given, it defaults:


#!/bin/ksh


filevar=$1
if [[ -z $filevar ]]
then
filevar="datafile.txt"
fi
.
.

Change the end of the while loop to this:

done < $filevar


Report Offensive Follow Up For Removal

Response Number 6
Name: Fouad
Date: April 17, 2008 at 03:05:53 Pacific
Subject: copy and past in a file
Reply: (edit)
I have run the following script:

#!/bin/ksh

filevar=$1
if [[ -z $filevar ]]
then
filevar="*.unm"
fi

while read line
do
length=${#line}
f10="$(echo "$line" | cut -c10-20)"
f1="$(echo "$line" | cut -c1-40)"
f2="$(echo "$line" | cut -c40-$len)"
printf "%s%s%s\n" "$f1" "$f10" "$f2"

done < $filevar

But I have got the result only on my screen and not saved in the same file.
Wht is going wrong?

Fouad


Report Offensive Follow Up For Removal

Response Number 7
Name: nails
Date: April 17, 2008 at 12:52:05 Pacific
Subject: copy and past in a file
Reply: (edit)
I don't know what you mean by "not saved in the same file", but here are two ways:

1) Since your script is not asking for any input, redirect your script as written to an output file. If your script is called myscript.ss, execute this:

myscript.ss filename > output.txt

2) Redirect the output of just the while loop by changing to this:

done < $filevar > output.txt


Report Offensive Follow Up For Removal

Response Number 8
Name: Fouad
Date: April 18, 2008 at 03:26:19 Pacific
Subject: copy and past in a file
Reply: (edit)
This script is a part off more larger script that those files checked, edit and transfer them to the database.
Let say I have a folder /myfolder/unmfiles
a lot off files with the ".unm" extention are saved there.
I want that this script check every unm file,
edit it in save it with the same name(redirect the output to the same file name).
That means the input file name = output file name.

Fouad


Report Offensive Follow Up For Removal

Response Number 9
Name: nails
Date: April 18, 2008 at 06:58:52 Pacific
Subject: copy and past in a file
Reply: (edit)
The shell doesn't allow editing files "in place" such as you've described. You'll have to rename the file once the loop completes such as this example:

done < $filevar > output.txt
mv output.txt $filevar

It's a good idea to copy the file until you're really sue the script works the way you expect:

cp output.txt $filevar

BTW, the modern scripting languages - such as perl - do allow in place file editing.


Report Offensive Follow Up For Removal

Response Number 10
Name: Fouad
Date: April 18, 2008 at 07:52:47 Pacific
Subject: copy and past in a file
Reply: (edit)
Thanks a lot Nails for your help and contribution.Very appreciate.

Have a nice weekend.

Fouad


Report Offensive Follow Up For Removal



Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: copy and past in a file

Comments:

 
  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 


Data Recovery Software



Version Tracker Pro
Keep your software current and secure, effortlessly

Click Here for a Free Scan

Driver Agent
Automatically find the latest drivers for your computer.
Click Here for a Free Scan



The information on Computing.Net is the opinions of its users. Such opinions may not be accurate and they are to be used at your own risk. Computing.Net cannot verify the validity of the statements made on this site. Computing.Net and Computing.Net, LLC hereby disclaim all responsibility and liability for the content of Computing.Net and its accuracy.
PLEASE READ THE FULL DISCLAIMER AND LEGAL TERMS BY CLICKING HERE

All content ©1996-2007 Computing.Net, LLC