Find and replace a part of the word in Shell


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Find and replace a part of the word in Shell
# 1  
Old 06-25-2007
Find and replace a part of the word in Shell

I have a csv file in which there are numbers like

078976/9XXX
098754/8XXX

I want to replace the XXX with null. I want to know the command/code to do this.
I know how to replace the whole word/number. But don't know how to replace a part of it.

Thanks in advance,
Mihir
# 2  
Old 06-25-2007
if you know its always going to be the last 3 characters,
Code:
 # echo "078976/9XXX"|sed 's/...$//g'
078976/9

# 3  
Old 06-25-2007
Code:
while read str
do
echo ${str%???}
done < file

# 4  
Old 06-25-2007
Another way always with sed :
Code:
$ cat seqs.txt
999999/5
123456/1XXX
010101/22B12
$ sed 's!\(/[0-9]\{1,\}\).*$!\1!' seqs.txt
999999/5
123456/1
010101/22
$

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Find Word in Column with replace (2)

Dear ALL, I have sample file : 4c:66:41:6b:b5:5f, 00:00,00:00, -22:-25, robert, 10.101.3.119, host1 4c:66:41:2c:c1:5a, 00:00,00:00, -21:-25, 10.101.3.112, host1 4c:66:41:6b:b1:5a, 00:00,00:00, -21:-25, Julia, 10.101.3.113, host1 4c:66:41:2c:c1:5b, 00:00,00:00, -21:-25, 10.101.3.115, host1... (2 Replies)
Discussion started by: gnulyn
2 Replies

2. Shell Programming and Scripting

Find Word in Column with replace

Hi ALL i have file.txt with text : 4c:66:41:6b:b5:5f, 00:00,00:00, -22:-25, users1, 10.101.3.119, host1 4c:66:41:2c:c1:5a, 00:00,00:00, -21:-25, 10.101.3.112, host1 4c:66:41:6b:b1:5e, 00:00,00:00, -21:-25, users1, 10.101.3.113, host1 4c:66:41:2c:c1:5b, 00:00,00:00, -21:-25, 10.101.3.115,... (3 Replies)
Discussion started by: gnulyn
3 Replies

3. Shell Programming and Scripting

Shell Script @ Find a key word and If the key word matches then replace next 7 lines only

Hi All, I have a XML file which is looks like as below. <<please see the attachment >> <?xml version="1.0" encoding="UTF-8"?> <esites> <esite> <name>XXX.com</name> <storeId>10001</storeId> <module> ... (4 Replies)
Discussion started by: Rajeev_hbk
4 Replies

4. Shell Programming and Scripting

Shell script - Replace just part of a single line in a file.....

Hey guy's.... I new here, But im working on a school project, and I am not really good at programming. In fact, this is the only programming class that I need because programming is not what I am majoring in. But I have everything done in this shell script except for this last part..... ... (9 Replies)
Discussion started by: hxdrummerxc
9 Replies

5. Shell Programming and Scripting

How to replace word with multiline text using shell scripting.

Hi all I have data files which contain data as shown below: Line 5: FIDE INST_DESC: DIAM Co Ltd/Japan => MAID Co Ltd/Japan INST_NME: DIAM Co Ltd/Japan => MAID Co Ltd/Japan Line 6: FIDE INST_DESC: DIAM DL/Pimco US Bond Open Born in the USA => MAID DL/Pimco US Bond Open Born in the... (6 Replies)
Discussion started by: Ganesh_more
6 Replies

6. Shell Programming and Scripting

Find and replace a word in all the files (that contain the word) under a directory

Hi Everyone, I am looking for a simple way for replacing all the files under a directory that use the server "xsgd1234dap" with "xsdr3423pap". For Example: In the Directory, $pwd /home/nick $ grep -l "xsgd1234dap" *.sh | wc -l 119 I have "119" files that are still using... (5 Replies)
Discussion started by: filter
5 Replies

7. Shell Programming and Scripting

simple Word Capitalization (Title) find/replace

Hi! I'm looking for a simple script, especially a one liner script in tcsh or bash that will emulate the find/replace in all text apps. I want to change all uppercase caracters to Title word (in wich only the first caracter is UpperCase and the rest is lowercase) I can use sed command, but... (2 Replies)
Discussion started by: sstpierre68
2 Replies

8. Shell Programming and Scripting

find replace a pattern and following characters in a word

Suppose that I have a string "one:#red two:#yellow three:#gr'een four:#blu^e" and I want to replace the pattern :# and the following characters in the word with nothing. The output string should look "one two three four" How can I do this with sed. Some points to consider (a) the last word in... (1 Reply)
Discussion started by: superuser84
1 Replies

9. Shell Programming and Scripting

How to find the count and replace the particular part of string in perl?

Hi, I am taking the current time using localtime function in perl. For example if the time is: #Using localtime $time = "12:3:10"; I have to replace the value 3 (03) i.e second position to be 03. The output should be: 12:03:10 But if the other string for example: $str:... (1 Reply)
Discussion started by: vanitham
1 Replies

10. UNIX for Dummies Questions & Answers

Seach for part of string and replace whole word

I am trying to find words in a text with a certain ending with sed and replace them with themselves but wrapped in tabs ex.: The fish swims in the water. -> searching for -ms ending The fish <tab>swims<tab>in the water. I've been trying all sorts of commands and get either an error... (5 Replies)
Discussion started by: stinnes
5 Replies
Login or Register to Ask a Question