Unix Shell - Remove the first 3 characters after reading each line

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > September 2006 > Remove the first 3 characters after reading each line





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author Remove the first 3 characters after reading each line
nicetom786@yahoo.com

2006-09-28, 1:20 pm

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
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`
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


pls guide.
Thanks

Stephan 'smg' Grein

2006-09-28, 1:20 pm

Maybe that works
cat jobs.txt | sed 's/\(^.\{3\}\)\(.*\)/\1/'


--
Stephan 'smg' Grein, <stephan at stephan minus rockt dot de>
https://stephangrein.de
GnuPG-Key-ID: 0xF8C275D4
FingerPrint: 5B6F 134A 189B A24D 342B 0961 8D4B 0230 F8C2 75D4
Chris F.A. Johnson

2006-09-28, 1:20 pm

On 2006-09-28, nicetom786@yahoo.com 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
Bill Marcum

2006-09-28, 1:20 pm

On 28 Sep 2006 09:33:24 -0700, nicetom786@yahoo.com
<nicetom786@yahoo.com> 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
> This prints XXXX_.
> I want JOBNAME to be printed .But length of JOBNAME differs in each
> line.
>

cat jobs.txt | cut -c5-
You don't need the cat; you could write
cut -c5- <jobs.txt


--
According to Kentucky state law, every person must take a bath at least
once a year.
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com