I want to remove 1st and last two characters of each line of the file
Ex: file1
zzfile1ee
@xfile2:y
qfile3>>
@ file4yy
and redirect to the file called new
Basically file will have any charcter including space, spical character...
Please help....
RudiC
August 5, 2015, 6:33am
2
Any attempts from your side?
Is this a homework assignment. Homework assignments must be submitted in the Homework & Coursework Questions forum and must contain a completely filled out homework template .
yes I tried below code.....
while read line
do
value=`echo $line |cut -c3-`
echo ${value%??} >>new
sleep 1
done<file1
Aia
August 5, 2015, 10:21pm
5
while read line; do
line="${line#??}"
line="${line%??}"
echo "$line"
done < file1 > new
or
perl -anlF"" -e 'print @F[2..$#F-2]' file1 > new
or
perl -pe 's/^..(.*)..$/$1/' file1 > new
or
perl -nl -e 'print substr($_, 2, -2)' file1 > new
Hi shell1509,
It looks like you had working code and Aia showed you a method for doing it three other ways (although none of Aia's suggestions do what you requested, hopefully they show you how it could be done more efficiently than invoking cut
for each line read from your input file).
Before we mark this thread solved, you might want to ask yourself what is supposed to happen if an input line contains less than four characters? And, do any of the suggestions provided so far do what you want in that case?
Longhand, quick and dirty, assumes a string length greater than 3 characters.
OSX 10.7.5, default bash terminal...
Last login: Thu Aug 6 21:09:11 on ttys000
AMIGA:barrywalker~> txt=' FILE1-+'
AMIGA:barrywalker~> text='#FILE2&*'
AMIGA:barrywalker~> echo "${txt:1:$((${#txt}-3))}"
FILE1
AMIGA:barrywalker~> echo "${text:1:$((${#text}-3))}"
FILE2
AMIGA:barrywalker~> _
EDIT:
Due to ambiguity in the OP's original post this assumes first TWO and last two characters.
AMIGA:barrywalker~> txt=' \FILE1-+'
AMIGA:barrywalker~> text='#$FILE2&*'
AMIGA:barrywalker~> echo "${txt:2:$((${#txt}-4))}"
FILE1
AMIGA:barrywalker~> echo "${text:2:$((${#text}-4))}"
FILE2
AMIGA:barrywalker~> _
This might address Don's concerns, depending on OP's intention:
sed 's/^.\{0,2\}//; s/.\{0,2\}$//' file