Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
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

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/kshwhile 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

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

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 3The 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

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

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

I have run the following script:
#!/bin/ksh
filevar=$1
if [[ -z $filevar ]]
then
filevar="*.unm"
fiwhile 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

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

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

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 $filevarIt'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.

![]() |
![]() |
![]() |

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