LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 02-15-2009, 05:59 AM   #1
alertroshannow
LQ Newbie
 
Registered: Feb 2009
Posts: 5

Rep: Reputation: 0
AWK a variable Ouptut to a new variable and using the new variable with the old one


Dear All,

I am trying to change a filename viz; ABC.txt, to ABC_utf8.txt
Once I get the _utf8* named file.. my directory should have two files, ABC.txt and ABC_utf8.txt

Thus later I would try and use "iconv" on ABC.TXT and change the char-set and generate "ABC_utf8.txt"

But all the time, my both variable store same value "ABC.txt"

find -name '*'\.\txt -type f -print|while read FILENAME
do
echo ${FILENAME}
FILENAME1= $(echo $FILENAME | awk '{c=split($0, s, "."); print "s[1]"_utf8."s[2]""}')
echo ${FILENAME1}
iconv -f windows-1256 -t UTF-8 "${FILENAME}" > "${FILENAME1}"
done


Please advise.
Pardon me if I was unable to explain my concern, I shall be glad to rephrase it again.

-Rgds,
Roshan Nair
 
Old 02-15-2009, 06:11 AM   #2
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950
I don't think you need to use awk for this. You can do the whole thing in one line with bash's parameter substitution. Assuming all input files have a ".txt" extension:

Code:
iconv -f windows-1256 -t UTF-8 "${FILENAME}" > "${FILENAME%.txt}_utf8.txt"
I just love parameter substitution.
 
Old 02-15-2009, 06:19 AM   #3
alertroshannow
LQ Newbie
 
Registered: Feb 2009
Posts: 5

Original Poster
Rep: Reputation: 0
Smile

Quote:
Originally Posted by David the H. View Post
I don't think you need to use awk for this. You can do the whole thing in one line with bash's parameter substitution. Assuming all input files have a ".txt" extension:

Code:
iconv -f windows-1256 -t UTF-8 "${FILENAME}" > "${FILENAME%.txt}_utf8.txt"
I just love parameter substitution.
Thank You So much David, it just clicked a happy pose for me

-Rgds,
Roshan
 
Old 02-15-2009, 07:14 AM   #4
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950
Glad to help. It's usually more productive to state the results you want, and let people give you possible solutions, than to restrict yourself to using a specific tool from the outset.

By the way, your original problem seems to be 1) an unwanted space between the "=" and the "$", and 2) some unnecessary quote marks. This works for me:

Code:
FILENAME1=$(echo $FILENAME | awk '{c=split($0, s, "."); print s[1]"_utf8."s[2]}')
But there are lighter tools you can use than awk, even if you don't go with parameter substitution (i.e. you're using a shell other than bash). Since it's just a simple text string substitution, how about sed?

Code:
FILENAME1=$(echo $FILENAME | sed "s/.txt/_utf8.txt/")
 
Old 02-16-2009, 01:08 AM   #5
alertroshannow
LQ Newbie
 
Registered: Feb 2009
Posts: 5

Original Poster
Rep: Reputation: 0
Thumbs up

I am actually an Oracle DBA, trying my hand on some of the shell scripting, to ensure that some of the manual tasks my developers do, are removed from their hectic responsibilities.

They generate a file (a flat file of encoding type ANSI) through Oracle (which is based on a HP Unix box) on a NAS Server running on windows, so I pick it from there, load it onto Linux convert the char-set to UTF8 and push it back to the NAS, so that they can have it as UTF8 as desired.

Your subsitution command did work for me, and I did edit the same in my code,

Thanks again.

Roshan Nair
 
  


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
Find variable name by variable content using a subshell UltramaticOrange Programming 3 11-17-2008 04:51 PM
Sed search for variable and delete entire line, but variable contains forward /'s Passions Programming 2 11-10-2008 04:44 PM
function showing a list of variable and value: (dynamic variable name) budhax Linux - Newbie 1 09-19-2008 08:05 PM
Help: removing a variable substring from a string variable in sh script gnparsons Programming 2 06-04-2008 06:21 PM
setting a variable variable in a script... this works, but could it be more elegant? pwc101 Programming 3 08-18-2006 12:23 PM


All times are GMT -5. The time now is 11:43 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration