Script to delete a word based on position in a file


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Script to delete a word based on position in a file
# 1  
Old 03-24-2011
Java Script to delete a word based on position in a file

Hi,

I am new to unix. I want to delete 2 words placed at position say for example at 23rd and 45th position in a line. I used sed but couldnt achieve this.

Example: the file contains 2 lines
Code:
12345 98765 "12345" 876
12345 98765 "64578" 876

I want to delete " placed at position 13 and 19 in both lines.

TIA for your reply

Last edited by Franklin52; 03-25-2011 at 06:46 AM.. Reason: Please use code tags
# 2  
Old 03-24-2011
Save file as: filename

Code:
sed -r "s/^(.{12})(.{1})/\1/" filename | sed -r "s/^(.{17})(.{1})/\1/"

The first sed:
/^(.{12})(.{1})/ means select 1 ((.{1})) character that comes after the first (^) 12 characters ((.{12}))
/\1/ means keep portion before the selection and replace the selection with nothing. That takes care of the first ".

The second sed simillarly removes the second " (which by the way is not in position 19 anymore, it's character number 18 and hence the option (.{17}))

-G
# 3  
Old 03-25-2011
thanks for your reply. but i am getting illegal option -- r error.
# 4  
Old 03-25-2011
Quote:
Originally Posted by nbks2u
thanks for your reply. but i am getting illegal option -- r error.
"-r" is not a standard option. It allows one to use extended regular expressions like the one we are using above.

Sorry, I don't know how to do it using basic reg-ex.

You might find some help from here:
https://www.unix.com/shell-programmin...oblem-aix.html

-G
# 5  
Old 03-25-2011
thank you.will check that. hope it solves my problem
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Search for a string at a particular position and replace with blank based on position

Hi, I have a file with multiple lines(fixed width dat file). I want to search for '02' in the positions 45-46 and if available, in that lines, I need to replace value in position 359 with blank. As I am new to unix, I am not able to figure out how to do this. Can you please help me to achieve... (9 Replies)
Discussion started by: Pradhikshan
9 Replies

2. Shell Programming and Scripting

Script needed to delete to the list of files in a directory based on last created & delete them

Hi My directory structure is as below. dir1, dir2, dir3 I have the list of files to be deleted in the below path as below. /staging/retain_for_2years/Cleanup/log $ ls -lrt total 0 drwxr-xr-x 2 nobody nobody 256 Mar 01 16:15 01-MAR-2015_SPDBS2 drwxr-xr-x 2 root ... (2 Replies)
Discussion started by: prasadn
2 Replies

3. Shell Programming and Scripting

Write a word at 72nd position of a matched line in a file

Hi, I need to search a file for a pattern,replace some other word and write a word at its 72nd position. For example, My name is Mano.Im learning Unix. I want to search the file in all lines containing the word "Mano".In that matched line,replace the word "Unix" with "Java".And... (5 Replies)
Discussion started by: mano1 n
5 Replies

4. Shell Programming and Scripting

Grep the 5th and 6th position character of a word in a file

I am trying to find/grep the 5th and 6th position character (TX) of a word in a file. I tried to do grep "....TX" file The output gives me everything in the file with TX in it. I only need the output with the TX in the 5th and 6th position of the word. Any idea Example: test1 car... (5 Replies)
Discussion started by: e_mikey_2000
5 Replies

5. Linux

Linux script to remove a character in a file based on position.

Greetings, We have a requirement where we need to loop in a fixed width file in linux and remove a character based on a position for every record. It would highly appreciate if someone can help to automate this. Appreciate your time and help! Regards (3 Replies)
Discussion started by: mailme0205
3 Replies

6. Shell Programming and Scripting

How to put a word starting at particular position in a file using shell scripting

Hi all, I'm new to shell scripting and hence this query. I have 2 files. temp.txt and config.txt. The values in temp.txt are tab separated. ex: temp.txt AB CDE GHIJ OPQRS WXY ex:config.txt (1st line for 1st element of temp.txt and so on) start = '1' end='5' start = '6' end =... (26 Replies)
Discussion started by: subhrap.das
26 Replies

7. UNIX for Dummies Questions & Answers

To Extract words from File based on Position

Hi Guys, While I was writing one shell script , I just got struck at this point. I need to extract words from a file at some specified position and do some comparison operation and need to replace the extracted word with another word. Eg : I like Orange very much. I need to replace... (19 Replies)
Discussion started by: kuttu123
19 Replies

8. UNIX for Dummies Questions & Answers

Paste a word in the third field position of a file

Hi All, I have a file like this, 0.0.0.1 /account 327706,Data Cleansing,,,CRM error,100,0 The above line is a comma separted data file. I want to modify the third field to The final data file should be like 0.0.0.1 /account 327706,Data Cleansing,,,CRM error,100,0 ... (1 Reply)
Discussion started by: girish.raos
1 Replies

9. Shell Programming and Scripting

Bash script to delete folder based on text file information

I have been working on a script to list all the name's of a subfolder in a text file then edit that text file and then delete the subfolder base on the edited text file so far I have been able to do every thing I just talked about but can't figure out how to delete the subfolers base on a text file... (8 Replies)
Discussion started by: bone11409
8 Replies

10. Shell Programming and Scripting

How to check a word position in a file ?

Hello everybody, I have a file like this : "window 1 truck 3 duck 2... fire 1... etc..." and I would like to print the following number of a word I am searching for. (For example here, if I search for the word "fire", I will print "1") Thank you for your help ! (7 Replies)
Discussion started by: tibo
7 Replies
Login or Register to Ask a Question