Delete word from the file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Delete word from the file
# 1  
Old 08-28-2015
Delete word from the file

My file has contents like below. It is named as output_file.txt

/usr/sas/sas_config/Lev1/SASApp/StoredProcessServer/Logs/SASApp_STPServer_2015-08-27_tmptcmsaslva2_19142.log
/usr/sas/sas_config/Lev1/SASApp/StoredProcessServer/Logs/SASApp_STPServer_2015-08-27_tmptcmsaslva2_19142.log
/usr/sas/sas_config/Lev1/SASApp/StoredProcessServer/Logs/SASApp_STPServer_2015-08-24_tmptcmsaslva2_18208.log
/usr/sas/sas_config/Lev1/SASApp/StoredProcessServer/Logs/SASApp_STPServer_2015-08-24_tmptcmsaslva2_19142.log
/usr/sas/sas_config/Lev1/SASApp/StoredProcessServer/Logs/SASApp_STPServer_2015-08-24_tmptcmsaslva2_29115.log

Now I would like to remove, /usr/sas/sas_config/Lev1/SASApp/StoredProcessServer/Logs/ from all the records. I need only the file names.

When I tried the commands like below, it is throwing an error. Kindly need some help.
Code:
Code:
-bash-4.1$ sed 's//usr/sas/sas_config/Lev1/SASApp/StoredProcessServer/Logs///g' output_file.txt > output.txt
sed: -e expression #1, char 8: unknown option to `s'
-bash-4.1$

# 2  
Old 08-28-2015
Why did you open a new thread instead of answering RudiC's question in your other thread: For loop - unexpected token `do

Thank you

EDIT:
Thanks for cleaning your keyboard, its easier to read now again.

---------- Post updated at 13:50 ---------- Previous update was at 13:48 ----------

And about your issue, try:
Code:
while read item
do  echo "${item##*/}"
done<output_file.txt

hth
# 3  
Old 08-28-2015
$ cat f1
Code:
/usr/sas/sas_config/Lev1/SASApp/StoredProcessServer/Logs/SASApp_STPServer_2015-08-27_tmptcmsaslva2_19142.log
/usr/sas/sas_config/Lev1/SASApp/StoredProcessServer/Logs/SASApp_STPServer_2015-08-27_tmptcmsaslva2_19142.log
/usr/sas/sas_config/Lev1/SASApp/StoredProcessServer/Logs/SASApp_STPServer_2015-08-24_tmptcmsaslva2_18208.log
/usr/sas/sas_config/Lev1/SASApp/StoredProcessServer/Logs/SASApp_STPServer_2015-08-24_tmptcmsaslva2_19142.log
/usr/sas/sas_config/Lev1/SASApp/StoredProcessServer/Logs/SASApp_STPServer_2015-08-24_tmptcmsaslva2_29115.log

$ cat f1.sh
Code:
#!/bin/sh

awk -F"/" '{ print $NF }' f1 > a.out

while read line
do
        program_name=`basename $line .log`
        echo $program_name
done < a.out
rm a.out

$ ./f1.sh
Code:
SASApp_STPServer_2015-08-27_tmptcmsaslva2_19142
SASApp_STPServer_2015-08-27_tmptcmsaslva2_19142
SASApp_STPServer_2015-08-24_tmptcmsaslva2_18208
SASApp_STPServer_2015-08-24_tmptcmsaslva2_19142
SASApp_STPServer_2015-08-24_tmptcmsaslva2_29115

This User Gave Thanks to amit2015 For This Post:
# 4  
Old 08-28-2015
When I run the script below,
Code:
#!/bin/sh
for i in "/usr/sas/sas_config/Lev1/SASApp/StoredProcessServer/Logs/SASApp_STPServer_"$(date +%Y-%m-%d --date "4 day ago")"_tmptcmsaslva2_"*".log"
do  
echo "${/usr/sas/sas_config/Lev1/SASApp/StoredProcessServer/Logs/##*/}"
done < output_file.txt

Got error as
Code:
line 4: ${/usr/sas/sas_config/Lev1/SASApp/StoredProcessServer/Logs/##*/}: bad substitution

My requirement was to have only the file name (instead of filename with directories) in output_file.txt

---------- Post updated at 07:20 AM ---------- Previous update was at 07:16 AM ----------

My script location and output file location is /usr/sas/tir/test/loganalysis.

I need to search for a file in /usr/sas/sas_config/Lev1/SASApp/StoredProcessServer/Logs/.

Now how to modify your script to include the directories?
# 5  
Old 08-28-2015
Quote:
Originally Posted by Ram Kumar_BE
When I run the script below,
Code:
#!/bin/sh
for i in "/usr/sas/sas_config/Lev1/SASApp/StoredProcessServer/Logs/SASApp_STPServer_"$(date +%Y-%m-%d --date "4 day ago")"_tmptcmsaslva2_"*".log"
do  
echo "${/usr/sas/sas_config/Lev1/SASApp/StoredProcessServer/Logs/##*/}"
done < output_file.txt

Got error as
Code:
line 4: ${/usr/sas/sas_config/Lev1/SASApp/StoredProcessServer/Logs/##*/}: bad substitution

My requirement was to have only the file name (instead of filename with directories) in output_file.txt
Hello Ram,

You should put $i as follows in code.
Code:
#!/bin/sh
for i in "/usr/sas/sas_config/Lev1/SASApp/StoredProcessServer/Logs/SASApp_STPServer_"$(date +%Y-%m-%d --date "4 day ago")"_tmptcmsaslva2_"*".log"
do  
       echo "${i##*/}"
done < output_file.txt

Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 6  
Old 08-28-2015
I got the required output in command prompt. However I would like redirect the result to file (eg.output_file.txt).

I would also request you to explain the echo statement
Code:
echo "${i##*/}"

as well.
# 7  
Old 08-28-2015
Hello Ram,

It is an example of parameter expansion, you can go through following.
Quote:
${parameter#word}${parameter##word}
The word is expanded to produce a pattern just as in filename expansion (see Filename Expansion). If the pattern matches the beginning of the expanded value of parameter, then the result of the expansion is the expanded value of parameter with the shortest matching pattern (the ‘#’ case) or the longest matching pattern (the ‘##’ case) deleted. If parameter is ‘@’ or ‘*’, the pattern removal operation is applied to each positional parameter in turn, and the expansion is the resultant list. If parameter is an array variable subscripted with ‘@’ or ‘*’, the pattern removal operation is applied to each member of the array in turn, and the expansion is the resultant list.


${parameter%word}${parameter%%word}
The word is expanded to produce a pattern just as in filename expansion. If the pattern matches a trailing portion of the expanded value of parameter, then the result of the expansion is the value of parameter with the shortest matching pattern (the ‘%’ case) or the longest matching pattern (the ‘%%’ case) deleted. If parameter is ‘@’ or ‘*’, the pattern removal operation is applied to each positional parameter in turn, and the expansion is the resultant list. If parameter is an array variable subscripted with ‘@’ or ‘*’, the pattern removal operation is applied to each member of the array in turn, and the expansion is the resultant list.
Thanks,
R. Singh
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Delete Word between two Char.

HI Guys, I have Input:- A nput A.txt 000100/port_xcu_dev_6/port_0_dev_7 000100/port_xcu_dev_6/port_1_dev_10 000100/port_xcu_dev_2/port_2_dev_8 000100/port_xcu_dev_3/port_3_dev_11 000100/port_xcuv_9/port_4_dev_9 ... (3 Replies)
Discussion started by: pareshkp
3 Replies

2. Shell Programming and Scripting

Search the word to be deleted and delete lines above this word starting from P1 to P3

Hi, I have to search a word in a text file and then I have to delete lines above from the word searched . For eg suppose the file is like this: Records P1 10,23423432 ,77:1 ,234:2 P2 10,9089004 ,77:1 ,234:2 ,87:123 ,9898:2 P3 456456 P1 :123,456456546 P2 abc:324234 (2 Replies)
Discussion started by: vsachan
2 Replies

3. UNIX for Dummies Questions & Answers

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 12345 98765 "12345" 876 12345 98765 "64578" 876 I want to delete " placed at position 13 and 19... (4 Replies)
Discussion started by: nbks2u
4 Replies

4. Shell Programming and Scripting

How to delete the last word in a file?

Hi All, I have a file with the data as below. In this i want to delete the last word. Could you pls help me. $INSTALL_HOME/lib/fm_voucher_pol.so $INSTALL_HOME/source/sys/fm_apn_pol/fm_apn_pol_device_set_state.c In the above two lines i want to delete fm_voucher_pol.so and... (5 Replies)
Discussion started by: girish.raos
5 Replies

5. Shell Programming and Scripting

Delete everything after/before a word in a file

I'm looking for a command that will read a file listing information and delete everything after a certain word is found. I also may need to search the file and delete everything before a certain word. The file would contains fields of information like below repeating for the entire file; Name... (5 Replies)
Discussion started by: daveisme
5 Replies

6. Shell Programming and Scripting

Delete character from a word

Friends, I'm looking for a command that delete the first tho caractere in a word. Here is an exp : I want to replace "20091001" by "091001" or "replace" by "place" Thx, (13 Replies)
Discussion started by: newpromo
13 Replies

7. Shell Programming and Scripting

To find a word in a file and delete the file

Hi, I need to find a word in a file, and I need to delete the file where the word resides on the file. I did try with the grep -H "word" using the find command, but no luck. (6 Replies)
Discussion started by: gsiva
6 Replies

8. Shell Programming and Scripting

Delete repeated word in text file

Hi expert, I am using C shell. And i trying to delete repeated word. Example file.txt: BLUE YELLOW RED VIOLET RED RED BLUE WHITE YELLOW BLACK and i wan store the output into a new file: BLUE (6 Replies)
Discussion started by: vincyoxy
6 Replies

9. UNIX for Dummies Questions & Answers

Delete a word

i have created the following input script CREATE OR REPLACE VIEW MMSSENDAVGUPLOADTIME_GN AS sp_strip_mean_value_type.get_val(MMSSENDAVGUPLOADTIME) MMSSENDAVGUPLOADTIME_val sp_strip_mean_value_type.get_val(MMSSENDAVGUPLOADTIME) MMSSENDAVGUPLOADTIME_counter select... (1 Reply)
Discussion started by: gseptember
1 Replies

10. Shell Programming and Scripting

How to delete a word from a file?

Hi All, I want to delete a word from file. How to do that. I have file that contains the following Information. EntityName:alba00r1.mis.amat.com OverallStatus:Minor IfName:Gi1/0 EntityName:alba00r1.mis.amat.com ] OverallStatus:Normal IfName:Se0/0/0... (4 Replies)
Discussion started by: ntgobinath
4 Replies
Login or Register to Ask a Question