removing 2 words from file.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting removing 2 words from file.
# 1  
Old 04-15-2010
removing 2 words from file.

Hi All,

I have a text file with name of source files in that. source files ends with .mxml and .css. Now I want to remove the extensions of these source files.

Currently I can do so by writing 2 sed commands, as there are files with just 2 different extensions. But I want to do it in one sed command.

Text file contains following lines.

Code:
MS_OFFICEDEPOT_GENERIC_10.css
Video_Collatorals_Resource_Center.mxml

I want output like this one.
Code:
MS_OFFICEDEPOT_GENERIC_10
Video_Collatorals_Resource_Center

Here is my code
Code:
sed 's|.css||' file
sed 's|.mxml||' file

I want to write one line code coz there can there 3 or 4 extensions...


Thanks in advance.

Regards
-Kashif
# 2  
Old 04-15-2010
you can try in awk
Code:
awk -F. '{print $1}' file_name

In Sed
Code:
sed 's/\..*//' file_name


Last edited by posix; 04-15-2010 at 04:55 AM.. Reason: sed code
# 3  
Old 04-15-2010
Got no nice solution with sed in mind without concatenating substition cause of different patterns so maybe this is ok for you, with plain shell including a grep:

Code:
grep -E "\.css|\.mxml" infile| while read LINE; do echo ${LINE%.*}; done
MS_OFFICEDEPOT_GENERIC_10
Video_Collatorals_Resource_Center

If there is only *.css and *.mxml files, you can leave the grep out of course.

---------- Post updated at 09:57 AM ---------- Previous update was at 09:46 AM ----------

Just for completeness here an sed example in one line but not much prettier than in two lines:
Code:
sed -e 's/\.css$//g; s/\.mxml$//g' infile
MS_OFFICEDEPOT_GENERIC_10
Video_Collatorals_Resource_Center


Last edited by zaxxon; 04-15-2010 at 04:57 AM.. Reason: correction, one infile too much
# 4  
Old 04-15-2010
MySQL

Code:
[root@sistem1lnx ~]# cat coz
MS_OFFICEDEPOT_GENERIC_10.css
Video_Collatorals_Resource_Center.mxml

MS_OFFICEDEPOT_GENERIC_10.bin
Video_Collatorals_Resource_Center.php

[root@sistem1lnx ~]# sed 's/\(.*\)\.\(.*\)/\1/g' coz
MS_OFFICEDEPOT_GENERIC_10
Video_Collatorals_Resource_Center

MS_OFFICEDEPOT_GENERIC_10
Video_Collatorals_Resource_Center

# 5  
Old 04-15-2010
Quote:
Originally Posted by zaxxon
[code]
grep -E "\.css|\.mxml" infile| while read LINE; do echo ${LINE%.*}; done
MS_OFFICEDEPOT_GENERIC_10
Video_Collatorals_Resource_Center
Isn't it the while loop enough....

Code:
while read LINE; do echo ${LINE%.*}; done < infile

# 6  
Old 04-15-2010
Hi this script can help u ...

##save the below code in a script , send filename as a parameter it will return the filename by removing the extension

Code:
#!/bin/ksh
filename=$1
pos=`expr index $filename . `
pos=`expr $pos - 1 `
rem_extn=`expr substr $filename  1  $pos `
echo "Actual Filename       : $filename"
echo "Filename without extn : $rem_extn"


Last edited by zaxxon; 04-15-2010 at 07:13 AM.. Reason: use code tags please, ty
# 7  
Old 04-15-2010
I was not sure if the OP has to search explicit for *.mxml and *.css or if there are other files with different suffixes as input.
Also some of the solutions do not work properly if there is more than 1 dot in the filename.
Also if there is only those 2 suffixes, I stated in my 1st post:
Quote:
If there is only *.css and *.mxml files, you can leave the grep out of course.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace particular words in file based on if finds another words in that line

Hi All, I need one help to replace particular words in file based on if finds another words in that file . i.e. my self is peter@king. i am staying at north sydney. we all are peter@king. How to replace peter to sham if it finds @king in any line of that file. Please help me... (8 Replies)
Discussion started by: Rajib Podder
8 Replies

2. Shell Programming and Scripting

Add words in beginning , end after removing a word in a file

My file has the entries like below... /dev/sds /dev/sdak /dev/sdbc /dev/sdbu I want to make the file like below echo 1 > /sys/block/sds/device/rescan echo 1 > /sys/block/sdak/device/rescan echo 1 > /sys/block/sdbc/device/rescan echo 1 > /sys/block/sdbu/device/rescan (2 Replies)
Discussion started by: saravanapandi
2 Replies

3. Shell Programming and Scripting

How count the number of two words associated with the two words occurring in the file?

Hi , I need to count the number of errors associated with the two words occurring in the file. It's about counting the occurrences of the word "error" for where is the word "index.js". As such the command should look like. Please kindly help. I was trying: grep "error" log.txt | wc -l (1 Reply)
Discussion started by: jmarx
1 Replies

4. Shell Programming and Scripting

Splitting concatenated words in input file with words from the same file

Dear all, I am working with names and I have a large file of names in which some words are written together (upto 4 or 5) and their corresponding single forms are also present in the word-list. An example would make this clear annamarie mariechristine johnsmith johnjoseph smith john smith... (8 Replies)
Discussion started by: gimley
8 Replies

5. Shell Programming and Scripting

removing the words with symbols in a file in unix

I have file like below Hi iam author <br>joseph</br> in france. I live in my home <br></br> but no food. I will play footbal <br></br> but i wont play cricket. I will read all the books <br>all fiction stories</br> i hate horror stories. I want output like below Hi iam author... (3 Replies)
Discussion started by: vinothsekark
3 Replies

6. Shell Programming and Scripting

Conditional removing of words from a line

Hi , I have a .csv file,from which I want to remove some data from each column as below. Source Data GT_12_AUDIT,SCHEDULED,NOZOMI2010/GT_12_AUDIT,CTSCAN/Zh_GT_6547887/GT_12_AUDIT,CTSCAN/Zh_GT_6547887... (3 Replies)
Discussion started by: gaur.deepti
3 Replies

7. Shell Programming and Scripting

finding and removing 2 identical consecutive words in a text

i want to write a shell script that correct a text file.for example if i have the input file: "john has has 2 apples anne has 3 oranges oranges" i want that the output file be like this: "john has 2 apples anne has 3 oranges" i've tried to read line by line from input text file into array... (11 Replies)
Discussion started by: cocostaec
11 Replies

8. Shell Programming and Scripting

Splitting Concatenated Words in Input File with Words from a Master File

Hello, I have a complex problem. I have a file in which words have been joined together: Theboy ranslowly I want to be able to correctly split the words using a lookup file in which all the words occur: the boy ran slowly slow put child ly The lookup file which is meant for look up... (21 Replies)
Discussion started by: gimley
21 Replies

9. Shell Programming and Scripting

Removing identical words in column

I have a file that needs to be cleaned up. Here is the file: Project Project John Project Gary Project Sean Project2 Project2 Lisa Project2 Tyler Project2 Sam Project3 Project3 Mike Project3 Bran I need the o/p to be: Project John Gary Sean Project2 (7 Replies)
Discussion started by: leepet01
7 Replies

10. Shell Programming and Scripting

Removing uppercase words from textfiles

I have the task of removing all uppercase words from csv files, mit 10000's lines. I think it shoud be possible with regex's, something like "s/{2,}//g" but I can't get it work with sed or Vi. It would also be possible to script in ksh, awk, perl or python. example this "this is a EXAMPLE... (5 Replies)
Discussion started by: frieling
5 Replies
Login or Register to Ask a Question