I want to remove 1st and last two characters of each line of the file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting I want to remove 1st and last two characters of each line of the file
# 1  
Old 08-05-2015
I want to remove 1st and last two characters of each line of the file

I want to remove 1st and last two characters of each line of the file

Ex: file1

Code:
zzfile1ee
@xfile2:y
 qfile3>>
@ file4yy

and redirect to the file called new

Basically file will have any charcter including space, spical character...

Please help....

Last edited by Don Cragun; 08-05-2015 at 08:12 AM.. Reason: Add CODE and ICODE tags.
# 2  
Old 08-05-2015
Any attempts from your side?
# 3  
Old 08-05-2015
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.
# 4  
Old 08-05-2015
Quote:
Originally Posted by RudiC
Any attempts from your side?
yes I tried below code.....

Code:
while read line
do
      value=`echo $line |cut -c3-`
      echo ${value%??} >>new
      sleep 1
done<file1

Moderator's Comments:
Mod Comment Please use CODE tags when displaying sample input, output, and code segments as required by forum rules.

Last edited by Don Cragun; 08-05-2015 at 11:23 PM.. Reason: Add CODE tags.
# 5  
Old 08-05-2015
Code:
while read line; do
    line="${line#??}"
    line="${line%??}"
    echo "$line"
done < file1 > new

or
Code:
perl -anlF"" -e 'print @F[2..$#F-2]' file1 > new

or
Code:
perl -pe 's/^..(.*)..$/$1/' file1 > new

or
Code:
perl -nl -e 'print substr($_, 2, -2)' file1 > new


Last edited by Aia; 08-06-2015 at 05:32 PM..
# 6  
Old 08-05-2015
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?
# 7  
Old 08-06-2015
Longhand, quick and dirty, assumes a string length greater than 3 characters.
OSX 10.7.5, default bash terminal...
Code:
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.
Code:
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~> _


Last edited by wisecracker; 08-06-2015 at 05:27 PM.. Reason: See above...
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to remove new line characters from data rows in a Pipe delimited file?

I have a file as below Emp1|FirstName|MiddleName|LastName|Address|Pincode|PhoneNumber 1234|FirstName1|MiddleName2|LastName3| Add1 || ADD2|123|000000000 2345|FirstName2|MiddleName3|LastName4| Add1 || ADD2| 234|000000000 OUTPUT : ... (1 Reply)
Discussion started by: styris
1 Replies

2. Shell Programming and Scripting

Remove first 2 characters and last two characters of each line

here's what im trying to do. i have a file containing lines similar to this: data.txt: 1hsRmRsbHRiSFZNTTA1dlEyMWFkbU5wUW5CSlIyeDFTVU5SYjJOSFRuWmpia0ZuWXpKV2FHTnRU 1lKUnpWMldrZFZaMG95V25oYQpSelEyWTBka2QyRklhSHBrUjA1b1kwUkJkd3BOVXpWM1lVaG5k... (5 Replies)
Discussion started by: SkySmart
5 Replies

3. Shell Programming and Scripting

Ksh: Read line parse characters into variable and remove the line if the date is older than 50 days

I have a test file with the following format, It contains the username_date when the user was locked from the database. $ cat lockedusers.txt TEST1_21062016 TEST2_02122015 TEST3_01032016 TEST4_01042016 I'm writing a ksh script and faced with this difficult scenario for my... (11 Replies)
Discussion started by: humble_learner
11 Replies

4. Shell Programming and Scripting

Remove entire line from a file if 1st column matches a pattern

I have one requirement to delete all lines from a file if it matches below scenario. File contains three column. Employee Number, Employee Name and Employee ID Scenario is: delete all line if Employee Number (1st column) contains below 1. Non-numeric Employee Number 2. Employee Number that... (3 Replies)
Discussion started by: anshu ranjan
3 Replies

5. Shell Programming and Scripting

Remove characters from line

I have a the following line 22.152.25.36 - K##### "GET /DGGKE/GetMail.do;jwebsphere=FADFFFGSFGSFGSDGFSDFGSDFGSDF HTTP/1.1" 200 44948 Need a cut command which should give me the below output 22.152.25.36 - K##### "GET /DGGKE/GetMail.do HTTP/1.1" 200 44948 Note: The value of jwebsphere can... (6 Replies)
Discussion started by: suindar1982
6 Replies

6. Shell Programming and Scripting

remove first few characters from each line

Hi, I have a file with lines like below. I need to remove first few characters from each line until a date format is found. 05/06/12 20:47:02 GUMGUY@98.192.174.74{42B42A72AC955F5926621273E3A15059.tomcat2}TP-Processor15 LogExchUsage: ERROR: 05/06/12 20:47:02... (8 Replies)
Discussion started by: ratheeshjulk
8 Replies

7. UNIX Desktop Questions & Answers

Remove new line characters from a file

I tried using below command tr -cd "" < InputFile.xml > output.txt ============= This removes all the tabs/newline/extra spaces from a file it successfully removed all the extra spaces,tabs and new line characters but then the complete file become one record. I want to retain one new line... (1 Reply)
Discussion started by: saini
1 Replies

8. Shell Programming and Scripting

need to remove 1st line of a file and save the file with same old name

Hi friends, I have a doubt, I am not sure whether it is possible ah nu. I am having a file(sample.txt) which contain 5 lines. I want to remove 1st line in the file and save the file with same old name (sample.txt). For removing 1st line i am using sed 1d filename But dono how to... (3 Replies)
Discussion started by: natraj005
3 Replies

9. Shell Programming and Scripting

sed to remove 1st two characters every line of text file

what is the sed command to remove the first two characters of every line of a text file? each line of the text file has the same amount of characters, and they are ALL NUMERIC. there are hundreds of lines though. for example, >cat file1.txt 10081551 10081599 10082234 10082259 20081134... (20 Replies)
Discussion started by: ajp7701
20 Replies

10. Shell Programming and Scripting

sed remove last 10 characters of a line start from 3rd line

hello experts, I need a sed command that remove last 10 characters of a line start from 3rd line. any suggestions? Thanks you (7 Replies)
Discussion started by: minifish
7 Replies
Login or Register to Ask a Question