• Post Reply Bookmark Topic Watch Topic
  • New Topic

how to parse a srting using awk command  RSS feed

 
saikrishna cinux
Ranch Hand
Posts: 689
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am new to unix shell scripting. Here i want to parse a file name as string and want to extract the name of the file using awk command

if i use find command i will get the complete path with the file name
1 ex: ./dir1/dir2/file1.txt
2 ex: ./dir1/dir5/dir10/dirx/diry/file1.txt

so the above examples are having different file path so i want to parse this string and i want to get the string after the last '/'

Please help me out ..
is there any command in SED /awk for this problem

Thanks for helping me int his issue
 
Andrew Monkhouse
author and jackaroo
Marshal Commander
Posts: 12156
256
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Didn't you get told about the basename command a few days ago?

Here is an awk statement that does the same thing:

Hopefully you will either look at the man pages and work out how this command works, or you will ask about anything you are confused about. If I were an instructor and someone handed in that as a solution to a homework assignment then I would ask them to explain it.

Regards, Andrew
 
Stefan Wagner
Ranch Hand
Posts: 1923
Linux Postgres Database Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You might use the find-option printf.


The \n is used to prevent the shell to overwrite it with its prompt.
If you just need the result, omit it:
 
saikrishna cinux
Ranch Hand
Posts: 689
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Andrew Monkhouse:
Didn't you get told about the basename command a few days ago?

Here is an awk statement that does the same thing:

Hopefully you will either look at the man pages and work out how this command works, or you will ask about anything you are confused about. If I were an instructor and someone handed in that as a solution to a homework assignment then I would ask them to explain it.

Regards, Andrew


Thanks Andrew , apology for asking very simple questions and annyoing you! :roll:
but here i am working hard reading man pages too and googling but i never get what i wanted to have
and i tried a lot using this logic
# Finds file in the current directory, (including its sub directories) if success then returns file name with extension else it returns null
fSearch()
{
echo $(find $1 -name $2 -type f)
count=$(find $1 -name $2 -type f | tr -cd '/' | wc -c)
echo $count

let lastField=count+1
echo $lastField

name=$(find $1 -name $2 -type f | cut -f $lastfield -d '/')
echo $name
}

path="."
fname=xy*.*
fSearch $path $fname
echo $name

It's working fine but complex.
so i asked for your help thanks a lot andrew!!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!