LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 06-16-2014, 08:53 AM   #1
unclesamcrazy
Member
 
Registered: May 2013
Posts: 189

Rep: Reputation: 1
Using awk in shell script


(a) I have a directory which contains sub-directories, sql files, text files, image files and other files.
I want to list only sql files, remove their file extension as well as remove spaces, dashes and dots from filename because filename is a name of database.
I have done this but it is not working.
Code:
find "/path/of/the/directory" -maxdepth 1 -name '*.sql' -type f -exec basename {} \; | sed -e 's/\.sql$//g' -e 's/ //g' -e 's/\.//g'
(b) I want to print ubuntu version number only.
Can we do it using awk only? I have tried it but I am doing with sed.
Code:
cat /etc/issue | awk '{print $2}' | sed 's/\..$//g'
Please help.
 
Old 06-16-2014, 08:56 AM   #2
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-20-live-usb/ aix
Posts: 5,106

Rep: Reputation: 873Reputation: 873Reputation: 873Reputation: 873Reputation: 873Reputation: 873Reputation: 873
(a) maybe this will work:
Code:
find "/path/of/the/directory" -maxdepth 1 -name '*.sql' -type f -exec sh -c "basename {} | sed -e 's/\.sql$//g' -e 's/ //g' -e 's/\.//g'"
(b) maybe uname will be better ?

Last edited by schneidz; 06-16-2014 at 09:49 AM.
 
Old 06-16-2014, 09:20 AM   #3
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,185

Rep: Reputation: 1586Reputation: 1586Reputation: 1586Reputation: 1586Reputation: 1586Reputation: 1586Reputation: 1586Reputation: 1586Reputation: 1586Reputation: 1586Reputation: 1586
a) you can use tr to remove certain characters from a set
Code:
find . -maxdepth 1 -name '*.sql' -type f -exec basename {} \; | tr -d ' .-_'
b)
Code:
lsb_release -a | awk '{print $2}'
 
1 members found this post helpful.
Old 06-16-2014, 09:49 AM   #4
unclesamcrazy
Member
 
Registered: May 2013
Posts: 189

Original Poster
Rep: Reputation: 1
Thanks for your answers.
@szboardstretcher
Thanks szboardstretcher, Actually, I can not use lsb_release -a because I want to store it in a variable like 12.04 or 13.10, I do not want to store 12.04.3 or 14.04.4 etc
your first solution worked.
Can you please help to do editing using awk like I have input 10.04.2 and I want it 10.04. I can do it using sed
Quote:
echo "10.04.2" | sed 's/\..$//g'
but I dont know how to do it using awk.
 
Old 06-16-2014, 09:57 AM   #5
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,185

Rep: Reputation: 1586Reputation: 1586Reputation: 1586Reputation: 1586Reputation: 1586Reputation: 1586Reputation: 1586Reputation: 1586Reputation: 1586Reputation: 1586Reputation: 1586
Treat it as a float:

Code:
echo "10.4.2" | awk '{printf "%.2f\n", $1}'
 
1 members found this post helpful.
Old 06-17-2014, 12:46 AM   #6
eklavya
Member
 
Registered: Mar 2013
Posts: 619

Rep: Reputation: 136Reputation: 136
Code:
echo "10.04.2" | awk -F"." '{OFS=".";} {print $1,$2;}'
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Shell variable in awk script rohini_sharma Programming 3 02-22-2010 08:32 AM
awk sed shell script slcalice Programming 3 03-09-2009 05:40 AM
awk shell script error ApacheRoseXbones Linux - Newbie 5 07-18-2008 12:06 PM
Shell Script / Awk help for a challenge cmontr Programming 30 06-03-2008 06:11 PM
Passing variables from AWK script to my shell script BigLarry Programming 1 06-12-2004 04:32 AM


All times are GMT -5. The time now is 05:04 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration