• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

how to parse a srting using awk command

 
Ranch Hand
Posts: 689
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author and jackaroo
Posts: 12200
280
Mac IntelliJ IDE Firefox Browser Oracle C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 1923
Scala Postgres Database Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!!
 
reply
    Bookmark Topic Watch Topic
  • New Topic