Computing.Net > Forums > Unix > copy and past in a file

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

copy and past in a file

Reply to Message Icon

Name: Fouad
Date: April 15, 2008 at 09:22:33 Pacific
OS: AIX 5.2
CPU/Ram: 4 / 8GB
Product: 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



Sponsored Link
Ads by Google

Response Number 1
Name: nails
Date: April 16, 2008 at 07:02:56 Pacific
Reply:

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



0

Response Number 2
Name: Fouad
Date: April 16, 2008 at 07:35:31 Pacific
Reply:

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


0

Response Number 3
Name: nails
Date: April 16, 2008 at 09:11:23 Pacific
Reply:

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


0

Response Number 4
Name: Fouad
Date: April 16, 2008 at 09:26:14 Pacific
Reply:

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


0

Response Number 5
Name: nails
Date: April 16, 2008 at 10:52:24 Pacific
Reply:

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


0

Related Posts

See More



Response Number 6
Name: Fouad
Date: April 17, 2008 at 03:05:53 Pacific
Reply:

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


0

Response Number 7
Name: nails
Date: April 17, 2008 at 12:52:05 Pacific
Reply:

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


0

Response Number 8
Name: Fouad
Date: April 18, 2008 at 03:26:19 Pacific
Reply:

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


0

Response Number 9
Name: nails
Date: April 18, 2008 at 06:58:52 Pacific
Reply:

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.


0

Response Number 10
Name: Fouad
Date: April 18, 2008 at 07:52:47 Pacific
Reply:

Thanks a lot Nails for your help and contribution.Very appreciate.

Have a nice weekend.

Fouad


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: copy and past in a file

deleting rows in a file www.computing.net/answers/unix/deleting-rows-in-a-file/3676.html

searching in a file www.computing.net/answers/unix/searching-in-a-file/4106.html

Largest file in a file system www.computing.net/answers/unix/largest-file-in-a-file-system/5500.html