Edit a text file

April 15, 2011 at 00:28:50
Specs: Windows XP
Hi,

I have a Text file. some records ends with 316byte length and some with 315. I want each to be end with 320 length. I just want to add space in end of each record.

IF a record ends with 315 then include 5spaces
IF a record ends with 316 then include 4spaces

Please help in this

thanks,
Umar


See More: Edit a text file

Report •

#1
April 15, 2011 at 07:06:26
Thsi might give yuo a hint:
#!/bin/bash

# min file length
ml=320

# set working directory
wd="/tmp/ttt"

# change to the working directory
cd "$wd"

# read filenames in working directory
for file in $(ls)
do
    # seperate file length from ls -l
    fl=$(ls -l $file | awk '{print $5}')

    # set x to current file length and check whether it's less than min file length
    for (( x = fl; x < ml; x++ ))
    do
        # echo whitespace to current file
        echo -n " " >> $file
    done
done

Click Here on HowTo ask good Question to get best Help
Let us know, if the problem is solved !!!


Report •

#2
April 16, 2011 at 17:44:09
Try this. Just change the file names to match the ones you have.

#!/bin/ksh

OLDIFS=$IFS
IFS="
"

exec 3< myfile
while read -u3 line
do
if [[ ${#line} -eq 315 ]]
then
line="$line "
else
line="$line "
fi
print ${line} >> myfile.new
done

mv myfile myfile.orig
mv myfile.new myfile

IFS=$OLDIFS


Report •

#3
April 17, 2011 at 22:18:44
@paulsep, please don't impart the wrong things to newbies. Don't parse files using `ls`. use shell expansion

for file in *
do
  ...
  size=$(stat -c "%s" $file)
 ...
done

to get the file size, use the stat command (or the find command).

Report •
Related Solutions


Ask Question