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.

-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$ 

[/CODE]

Why did you open a new thread instead of answering RudiC's question in your other thread: http://www.unix.com/shell-programming-and-scripting/260424-loop-unexpected-token-do-2.html\#post302953450

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:

while read item
do  echo "${item##*/}"
done<output_file.txt

hth

$ cat f1

/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

#!/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

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


1 Like

When I run the script below,

#!/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

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?

Hello Ram,

You should put $i as follows in 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

1 Like

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

echo "${i##*/}"

as well.

Hello Ram,

It is an example of parameter expansion, you can go through following.

Thanks,
R. Singh

To get output in a file, put it in a file.

while something
do
...
done < file > outfile

I've one last question.

With this code I can get a .txt file as required. However, I would like to get a fresh file everyday instead of the appending the the same .txt file.

#!/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##*/}" < output_file.txt
done 

e.g. If I run today I may get 3 records in output_file.txt. When I run tomorrow I should not get the same records along with current day's records in output_file.txt rather I should get only current day's records in output_file.txt

Hello Ram,

There are two ways to complete this request.
I- Keep getting a output file which has particular day's date in it's name itself, so each day files will be unique and different names.

#!/bin/sh
DATE=`date +%y%m%d`
 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##*/}" >>  "output_file_"$DATE".txt"
done
 

II- Each day check for file named output_file.txt and rename it to that's day's date and create a output file with name output_file.txt.

#!/bin/sh
DATE=`date +%y%m%d`
if [[ -f output_file.txt ]]
then
      mv output_file.txt "output_file_"$DATE".txt"
fi
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##*/}" >>  output_file.txt
done
 

Thanks,
R. Singh

1 Like

There is other way which I found now:). Just I put a remove command at the beginning of the script like below.

#!/bin/sh
rm output_file.txt
for i in "/usr/sas/sas_config/Lev1/SASApp/StoredProcessServer/Logs/SASApp_STPServer_"$(date +%Y-%m-%d --date "1 day ago")"_tmptcmsaslva2_"*".log"
do
    echo "${i##*/}" >> output_file.txt
done

Now I'm looking to capture the script execution in log files (.log)

With this code snippet, you won't get any output_file.txt . Replacing < with > , you'd get an output_file.txt with exactly the last .log file name encountered.
Putting the > output_file.txt right after the done , as proposed by Corona688 in post#8, you'll have a new output file every time you run the snippet.

Script is working fine. Earlier there was a line feed issue.

Now I was looking to get a script execution log file.