Hi,
I would like to extract a substring from a string in unix.
eg:
./checkfile.sh -- i need only checkfile.sh from this string.
Could someone help me out in this...
Regards
Arun
Hi,
I would like to extract a substring from a string in unix.
eg:
./checkfile.sh -- i need only checkfile.sh from this string.
Could someone help me out in this...
Regards
Arun
echo "./checkfile.sh "| cut -d '/' -f2
or
echo "./checkfile.sh " | awk -F"/" ' { print $2 } '
Thanks Amit.
In my case, when i try to find the files in a directory and assign it to a variable using this command -
listfiles=`find . -name "*sh" -newer TODAY`
echo $listfiles
i get the output as below -
./file_check.sh
./check.sh
When i try either of the below commands -
echo "listfiles "| cut -d '/' -f2
or
echo "listfiles " | awk -F"/" ' { print $2 } '
the output i am getting is - file_check.sh .
I should get file_check.sh checkfile.sh
Could you please help me in this?
Regards
Arun
Now redirect the out put to a file for the find command
find . -name "*sh" -newer TODAY > temp.txt
awk -F"/" ' { print $2 } ' temp.txt
Thank u so much Amit.
Hi Amit,
In this code,
find . -name "*sh" -newer TODAY > temp.txt
awk -F"/" ' { print $2 } ' temp.txt
We are getting field 2 and printing it in the terminal.
Will it be possible to resend the field 2 values back to the same file temp.txt replacing the existing data in that file?
Regards
Arun
this could be a solution:
for file in $(find . -name "*sh" -newer TODAY)
do
echo $file | cut -d/ -f2
done
But if you want to use the script from any directory (not actual only):
search_path=$1
for file in $(find $search_path -name "*sh" -newer TODAY)
do
echo $file | awk -F"/" '{ print $NF}'
done
check
basename
Hi Albert,
When i add these in my script, i am getting this exception -
syntax error at line 6: `$' unexpected
Regards
Arun
sorry, i put the variable search_path with de first parameter when calling de script.
If you want to search in "." (actual dir) replase $search_path for .
The idea was that if you want to search in (i.e) /home/user1/scripts you call the script with:
name_script /home/user1/scripts
I hope I explain..
Hi Albert,
But I get the exception in the for loop itself.
for file in $(find . -name "*sh" -newer TODAY)
---------- Post updated at 09:11 AM ---------- Previous update was at 09:08 AM ----------
Hi Madan,
I tried with this
basename $file
. but it is not working...
My output from the find command comes like this and i redirect it to a file -
Here is the output -
./CLHALL02.csv
./CTHALL02.csv
./DV.csv
./ODHALL02.csv
Now i need this output without "./" which should be redirected to the same file which i will use later in my script.
basename is really what you need.
leion@Leionubuntu:~$ basename ./data
data
leion@Leionubuntu:~$ basename /home/leion/data
data
leion@Leionubuntu:~$
basename will pretty much do what you need, can you please post the full command that you have used?
Hi Madan,
here is my command,
find . -name "*csv" -newer TODAY > temp1.txt
Where can i use the basename option in the above command?
find . -name "*csv" -newer TODAY -exec basename {} \; > temp1.txt
Hi Madan,
I tried this and it worked fine...
In another case, i need to find the files and also list them.. how can i use basename in that?
find . -name "*csv" -newer /auto/users-35/p494856/learning/scripts/TODAY -exec ls -ltr {} \; > temp.txt
I am getting the output as -
-rw-r--r-- 1 p494856 dp 2852189 May 21 00:15 ./CTHALL02.csv
-rw-r--r-- 1 p494856 dp 35786 May 21 00:02 ./DV.csv
Will it be possible to remove the "./" from here and post the result back to the same file?
If so where should i make the change in the above code?
Regards
Arun
Hi,
try with
ls -tr1
instead of
ls -ltr
I don't understand the error caused before....:(...but the solution of Matrixmadham is better...
Hi Albert,
find . -name "*csv" -newer /auto/users-35/p494856/learning/scripts/TODAY -exec ls -trl {} \; > temp.txt
This is working fine and i'm not getting any errors. But I need to remove the "./" characters from the output...
Here is the output i'm getting now -
-rw-r--r-- 1 p494856 dp 1868503 May 21 00:11 ./CLHALL02.csv
-rw-r--r-- 1 p494856 dp 2852189 May 21 00:15 ./CTHALL02.csv
-rw-r--r-- 1 p494856 dp 35786 May 21 00:02 ./DV.csv
-rw-r--r-- 1 p494856 dp 14582 May 21 00:06 ./ODHALL02.csv
Madan suggested to use basename option to remove the "./"... but i'm not sure where to use it in the above code.
When i try without the ls -trl option, i can use the basename like below -
find . -name "*csv" -newer /auto/users-35/p494856/learning/scripts/TODAY -exec basename {} \; > temp.txt
will it be possible to have both ls -trl and basename in the same code?
Regards
Arun
find . -name "*csv" -newer /auto/users-35/p494856/learning/scripts/TODAY -exec basename {} \; | awk '{print $1 "\t" $2 "\t" $3 "\t" $4 "\t" $5 "\t" $6 "\t" $7 "\t" $8 "\t" substr($9,3,10000) }'
It's not very beautiful...but
I would like to know how put "the max lenght of the nineth field" instead of 100000 or a big number...
---------- Post updated at 02:19 PM ---------- Previous update was at 02:17 PM ----------
Sorry....put only:
substr($9,3)
this way shows the field from third character to the end!
Hi Albert,
I am getting only the file names.
CLHALL02.csv
CTHALL02.csv
DV.csv
ODHALL02.csv
I'm not getting the details of the files like -
-rw-r--r-- 1 p494856 dp 1868503 May 21 00:11 CLHALL02.csv
-rw-r--r-- 1 p494856 dp 2852189 May 21 00:15 CTHALL02.csv
-rw-r--r-- 1 p494856 dp 35786 May 21 00:02 DV.csv
-rw-r--r-- 1 p494856 dp 14582 May 21 00:06 ODHALL02.csv