Re: Remove the first 3 characters after reading each line



On 2006-09-28, nicetom786@xxxxxxxxx wrote:
Hi ,
I am new to unix .
I have a file to read and after reading each line I want to strip off
the first 3 characters and store in another variable.
Cut command cuts the first 3 characters but I do not how to get the
rest of letters.
For ex:
I have a line XXX_JOBNAME
Here is the code
cat jobs.txt |cut -c1-4

Thyere is no need for cat:

cut -c1-4 jobs.txt

This prints XXXX_.
I want JOBNAME to be printed .But length of JOBNAME differs in each
line.


#Logic

ext=".ksh"
for line in `cat ~/jobs.txt`

That is almost always the wrong way to read a file. You are reading
it word by word, not line by line.

do
echo $line
#remove the XXX_
#put the cut logic here after ripping of 3
characters and strore in another variale "job"

#concanat .ksh to each job = $job
job="$line$ext"
echo $job
#copy from SRC to taget
#cp ~/src/job.ksh ~/target
done

while IFS= read -r line
do
right=${line#????}
left=${line%"$right"}

: ....
done < jobs.txt


If it's a large file, awk may be faster:

awk '{
left = substr( $0, 1, 4 )
right = substr( $0, 5 )

}' jobs.txt

--
Chris F.A. Johnson, author <http://cfaj.freeshell.org/shell>
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence
.



Relevant Pages

  • Re: uniq without sort <-------------- GURU N
    ... magical powers. ... Note that awk will read from the pipe the same way as it would ... whether it can open a file for reading. ... In any case using "cat" here doesn't make sense as cat is the ...
    (comp.unix.shell)
  • Re: Stupid cat tricks - Anyone have other tricks?
    ... Note that tr, sed, awk may fail on files containing '\0' ... characters. ... No cat at all is sufficient. ...
    (comp.unix.shell)
  • Re: Rita Mae Brown
    ... Rita Mae Brown has written many, many books on very diverse subjects. ... The cat mysteries written with Sneaky Pie Brown, ... However her animal characters and her plots ...
    (rec.pets.cats.anecdotes)
  • Re: Little C Help Please
    ... characters to read, so it hapily goes on reading even when the buffer ... represent the size of the buffer, and stops reading before that number ... I think the OP's problem was not a space in the input, but a newline ... Dig the even newer still, yet more improved, sig! ...
    (comp.os.linux.development.apps)
  • Re: Little C Help Please
    ... characters to read, so it hapily goes on reading even when the buffer ... represent the size of the buffer, and stops reading before that number ... I think the OP's problem was not a space in the input, but a newline ... Dig the even newer still, yet more improved, sig! ...
    (comp.unix.programmer)