to delete charecter in a line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting to delete charecter in a line
# 1  
Old 01-20-2010
to delete charecter in a line

Hi,
Using perl, how can I delete a charecter (specify the position number), in a line which starts with "GETR P" and another line starting with "GET P:=". This needs tro be done for many charecters in a line

Thanks
Vijayalakshmi
# 2  
Old 01-20-2010
Code:
cat abc.txt | perl -e 'while(<>){
chomp;
my $pos_to_remove = 4; # change this position as per ur req
print substr($_,0,($pos_to_remove-1)).substr($_,$pos_to_remove)."\n";
}'

Alternatively you can use a simple regex as well

Code:
 cat abc.txt | perl -e 'while(<>){ chomp;
my $pos_to_remove = 3;
s/(.{$pos_to_remove}).(.*)/$1$2/g;
print "$_\n";
}'


HTH,
PL
# 3  
Old 01-20-2010
Input file:

Code:
$ cat t
GETR P123456
GET P:=12345
sdflkj sdfkj

Code:
Code:
$ cat t.pl
while(<>)  {
s/^((GETR P|GET P:=).{3}).(.*)$/\1\3/;
print;
}

Execute it like this:
Code:
perl -i t.pl t

After the pattern, the 4th character is deleted. Give appropriate number for your requirement.
Code:
$ cat t
GETR P12356
GET P:=1235
sdflkj sdfkj

-------------

Oops, a similar approach as above.. but with the string start is taken care here.
Also, get that variable $pos_to_remove and use here...

Last edited by thegeek; 01-20-2010 at 02:46 AM.. Reason: seen the above reply and small edits.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Mismatched free() / delete / delete [] line no missing

Could you tell me the possibilities of the reason to get the Mismatched free() / delete / delete . I unable to see the line no in the valgrind report. it displays the function name. with that function name, I am not able to find where exactly the issue is there.I am getting the Mismatched free()... (3 Replies)
Discussion started by: SA_Palani
3 Replies

2. UNIX for Advanced & Expert Users

How to find a string in a line in UNIX file and delete that line and previous 3 lines ?

Hi , i have a file with data as below.This is same file. But actual file contains to many rows. i want to search for a string "Field 039 00" and delete that line and previous 3 lines in that file.. Can some body suggested me how can i do using either sed or awk command ? Field 004... (7 Replies)
Discussion started by: vadlamudy
7 Replies

3. Shell Programming and Scripting

sed command to grep multiple pattern present in single line and delete that line

here is what i want to achieve.. i have a file with below contents cat fileName blah blah blah . .DROP this REJECT that . --sport 7800 -j REJECT --reject-with icmp-port-unreachable --dport 7800 -j REJECT --reject-with icmp-port-unreachable . . . more blah blah blah --dport 3306... (14 Replies)
Discussion started by: vivek d r
14 Replies

4. Shell Programming and Scripting

Delete line with match and previous line quoting/escaping problem

Hi folks, I've list of LDAP records in this format: cat cmmac.export.tmp2 dn: deviceId=0a92746a54tbmd34b05758900131136a506,ou=devices,ou=customer,ou=nl,o=upc cmmac: 00:13:11:36:a5:06 dn: deviceId=0a92746a62pbms4662299650015961cfa23,ou=devices,ou=customer,ou=nl,o=upc cmmac:... (4 Replies)
Discussion started by: tomas.polak
4 Replies

5. Shell Programming and Scripting

Delete last charecter of each line.

Hi, I have file which is having the following content. 11082202^M 10951265^M 11482003^M 12820986^M 11309561^M 11092815^M 11103308^M 13024722^M 11343958^M 11280355^M 7516723^M i want to delete the last charecter "^M" from all the line and redirect to other line. Please help... (1 Reply)
Discussion started by: shivanete
1 Replies

6. Shell Programming and Scripting

how to delete text from line starting pattern1 up to line before pattern2?

My data is xml'ish (here is an excerpt) :- <bag name="mybag1" version="1.0"/> <contents id="coins"/> <bag name="mybag2" version="1.1"/> <contents id="clothes"/> <contents id="shoes"/> <bag name="mybag3" version="1.6"/> I want to delete line containing mybag2 and its subsequent... (5 Replies)
Discussion started by: repudi8or
5 Replies

7. UNIX for Dummies Questions & Answers

charecter or number

Hi Friends, How to check a variable value is either charecter or number? With Regards / Ganapati (1 Reply)
Discussion started by: ganapati
1 Replies

8. Shell Programming and Scripting

find charecter from its ascii value.

how could find charecter from its ascii value. please suggest me any code for that. thannks in advance. (2 Replies)
Discussion started by: rinku
2 Replies

9. Shell Programming and Scripting

Comapring files charecter by charecter using AWK/Shell Script

Hi... I have a requrement to compare two files. for e.g. File 1 2007/08/19 09:48:10 DH-032 $APTA1: Device AATD8029 2007/08/19 09:48:10 DH-045 $APTA1: Device AATD8029 2007/08/19 09:48:10 DH-043 $APTA1: Device AATD8029 File 2 2007-08-19 09:48:10 DH-032... (1 Reply)
Discussion started by: evvander
1 Replies

10. Shell Programming and Scripting

cut First charecter in any string

I wannt to cut first char of any string. str=A2465443 out put will be. str1=A str2=2465443. I tried str2=${?%srt} str1=${str#$str2} it is not working. (6 Replies)
Discussion started by: rinku
6 Replies
Login or Register to Ask a Question