LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-01-2011, 09:36 PM   #1
marq_new
LQ Newbie
 
Registered: May 2011
Posts: 1

Rep: Reputation: 0
variable string file base


Hi,
I'm a new linux programmer. can you help me with my script, please.

this is may script:


Code:
IDP_CODE=`cat $RefTbl"idp_code.txt"|sort|uniq`

#dialed digit with "*" character

cat $INPUTRaw$DDay1$CHour1$CMin2|awk -F~ '{print $22":"$11","$12","$13","$22","$
7","$26"~"$27","$28","$29","$35","$36","$37","$38","$44}'|awk -F, '$3 ~ /^639|^9
/'|awk -F, '$1 ~ /*/'|sort >>  $OUTPUTDir"IDP/Temp.txt"

cat $INPUTRaw$PDay1$PHour1$PMin1|awk -F~ '{print $22":"$11","$12","$13","$22","$
7","$26"~"$27","$28","$29","$35","$36","$37","$38","$44}'|awk -F, '$3 ~ /^639|^9
/'|awk -F, '$1 ~ /*/'|sort >>  $OUTPUTDir"IDP/Temp.txt"


#dialed digit with "#" character
cat $INPUTRaw$DDay1$CHour1$CMin2|awk -F~ '{print $22":"$11","$12","$13","$22","$
7","$26"~"$27","$28","$29","$35","$36","$37","$38","$44}'|awk -F, '$3 ~ /^639|^9
/'|awk -F, '$1 ~ /#/'|sort >>  $OUTPUTDir"IDP/Tempb.txt"
cat $INPUTRaw$PDay1$PHour1$PMin1|awk -F~ '{print $22":"$11","$12","$13","$22","$
7","$26"~"$27","$28","$29","$35","$36","$37","$38","$44}'|awk -F, '$3 ~ /^639|^9
/'|awk -F, '$1 ~ /#/'|sort >>  $OUTPUTDir"IDP/Tempb.txt"

for d in $IDP_CODE
 do
   #dialed digit with "*" character
   cat $OUTPUTDir"IDP/Temp.txt"|grep -av ^$d"0907"|grep -av ^$d"0908"|grep -av ^
$d"0909"|grep -av ^$d"0910"|grep -av ^$d"0912"|grep -av ^$d"0918"|grep -av ^$d"0
919"|grep -av ^$d"0920"|grep -av ^$d"0921"|grep -av ^$d"0928"|grep -av ^$d"0929"
|grep -av ^$d"0930"|grep -av ^$d"0938"|grep -av ^$d"0939"|grep -av ^$d"0948"|gre
p -av ^$d"0949"|grep -av ^$d"0999"|uniq >> $OUTPUTDir"IDP/re_extract_"$DDay1$CHo
ur1$CMin".txt"
   mv $OUTPUTDir"IDP/re_extract_"$DDay1$CHour1$CMin".txt" $OUTPUTDir"IDP/Temp.tx
t"
done

#apped all trans with # character
cat $OUTPUTDir"IDP/Tempb.txt" >> $OUTPUTDir"IDP/Temp.txt"
cat $OUTPUTDir"IDP/Temp.txt"|awk -F: '{print $2}'|awk -F~ '{print $1":"$2}'|uniq > $OUTPUTDir"IDP/re
_extract_"$DDay1$CHour1$CMin".txt"

Problem: Output file doest not exclude the values in grep -av

thanks in advance.

Last edited by XavierP; 05-02-2011 at 04:47 AM. Reason: Moved to Programming
 
Old 05-02-2011, 08:08 AM   #2
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
I'm not even going to try to guess without some more background info. Care to explain what your script is supposed to do?

How about giving us an example of the input it's supposed to be processing? What is the expected output, and what is the actual output you get? What else do we need to know to understand what the script is doing? Be specific.

Also, your formatting is highly unreadable. I had to take several minutes just to break it up into something more easily understandable. Here it is with each command given its own line, but with no other changes made.
Code:
#!/bin/bash

IDP_CODE=` cat $RefTbl"idp_code.txt" | sort | uniq `

#dialed digit with "*" character

cat $INPUTRaw$DDay1$CHour1$CMin2 | \
awk -F~ '{ print $22":"$11","$12","$13","$22","$7","$26"~"$27","$28","$29","$35","$36","$37","$38","$44 }' | \
awk -F, '$3 ~ /^639|^9/' | \
awk -F, '$1 ~ /*/' | \
sort >>  $OUTPUTDir"IDP/Temp.txt"

cat $INPUTRaw$PDay1$PHour1$PMin1 | \
awk -F~ '{ print $22":"$11","$12","$13","$22","$7","$26"~"$27","$28","$29","$35","$36","$37","$38","$44 }' | \
awk -F, '$3 ~ /^639|^9/' | \
awk -F, '$1 ~ /*/' | \
sort >>  $OUTPUTDir"IDP/Temp.txt"


#dialed digit with "#" character

cat $INPUTRaw$DDay1$CHour1$CMin2 | \
awk -F~ '{ print $22":"$11","$12","$13","$22","$7","$26"~"$27","$28","$29","$35","$36","$37","$38","$44 }' | \
awk -F, '$3 ~ /^639|^9/' | \
awk -F, '$1 ~ /#/' | \
sort >>  $OUTPUTDir"IDP/Tempb.txt"

cat $INPUTRaw$PDay1$PHour1$PMin1 | \
awk -F~ '{ print $22":"$11","$12","$13","$22","$7","$26"~"$27","$28","$29","$35","$36","$37","$38","$44 }' | \
awk -F, '$3 ~ /^639|^9/' | \
awk -F, '$1 ~ /#/' | \
sort >>  $OUTPUTDir"IDP/Tempb.txt"

for d in $IDP_CODE ; do

	#dialed digit with "*" character

	cat $OUTPUTDir"IDP/Temp.txt" | \
	grep -av ^$d"0907" | \
	grep -av ^$d"0908" | \
	grep -av ^$d"0909" | \
	grep -av ^$d"0910" | \
	grep -av ^$d"0912" | \
	grep -av ^$d"0918" | \
	grep -av ^$d"0919" | \
	grep -av ^$d"0920" | \
	grep -av ^$d"0921" | \
	grep -av ^$d"0928" | \
	grep -av ^$d"0929" | \
	grep -av ^$d"0930" | \
	grep -av ^$d"0938" | \
	grep -av ^$d"0939" | \
	grep -av ^$d"0948" | \
	grep -av ^$d"0949" | \
	grep -av ^$d"0999" | \
	uniq >> $OUTPUTDir"IDP/re_extract_"$DDay1$CHour1$CMin".txt"

	mv $OUTPUTDir"IDP/re_extract_"$DDay1$CHour1$CMin".txt" $OUTPUTDir"IDP/Temp.txt"

done

#apped all trans with # character
cat $OUTPUTDir"IDP/Tempb.txt" >> $OUTPUTDir"IDP/Temp.txt"
cat $OUTPUTDir"IDP/Temp.txt" | \
awk -F: '{ print $2 }' | \
awk -F~ '{ print $1":"$2 }' | \
uniq > $OUTPUTDir"IDP/re_extract_"$DDay1$CHour1$CMin".txt"
I can already predict that there's a lot of unnecessary duplication of commands here. Indeed, if we knew what it's supposed to be doing, it's likely the whole thing could be done entirely in a single awk script.

A few points that stand out though to start with.

1) $(..) is recommended over `..`

2) sort has a -u option that does the same thing as uniq.

3) Your script doesn't include definitions for most of the variables used. For example, I'm assuming that $INPUTRaw$DDay1$CHour1$CMin2 references a file name. What's in it? Where are the components that make up this string defined? There must be more to this script than what you've shown.

4) Why use the -a option in grep anyway? That's only used for processing binary files, but your script makes it clear that the input is text. Again, what's the intended output of the command?

5) (added) It looks to me like you have one big awk code block duplicated 4 times. That means that the command could easily be redefined as a single function, at the very least. Write once, use many.

Last edited by David the H.; 05-02-2011 at 08:17 AM. Reason: addendum
 
1 members found this post helpful.
Old 05-02-2011, 09:23 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
+1 to all that David said.
 
Old 05-04-2011, 07:27 AM   #4
archtoad6
Senior Member
 
Registered: Oct 2004
Location: Houston, TX (usa)
Distribution: MEPIS, Debian, Knoppix,
Posts: 4,727
Blog Entries: 15

Rep: Reputation: 234Reputation: 234Reputation: 234
+2 to David.

marq_new,

Welcome to LQ. Glad to see you jump in w/ a Programming question.
Also, thanks for using "Code:" blocks, at all -- they're much easier to read than the alternatives.

Last edited by archtoad6; 05-04-2011 at 07:31 AM.
 
  


Reply

Tags
[awk], [bash]



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
[SOLVED] getting sub-string in a variable tedy2808 Linux - Newbie 75 10-14-2010 01:23 AM
Sed/awk/grep search for number string of variable length in text file Alexr Linux - Newbie 10 01-19-2010 01:34 PM
Help: removing a variable substring from a string variable in sh script gnparsons Programming 2 06-04-2008 05:21 PM
extract string from file to variable [BASH] NikosNL Programming 13 05-07-2008 09:43 AM
variable to string x2000koh Programming 4 07-30-2003 02:23 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:47 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
Open Source Consulting | Domain Registration