Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

passing special character in awk

Status
Not open for further replies.

Dvaderanakin

Programmer
Oct 27, 2002
9
MY
How do awk recognize user input such as "Record (1)"??
I have a script that search an inputed fields by the user. The script search the fields and display what's in between the entered fields.

Sample data:-

Record (1) "ML_REC"
"REC_SEQ_NUMBER" = "000442"
"CALL_ID" = "00000000"
"REC_NUMBER" = "01"
"CAUSE_FOR_OUTPUT" = "04"
"A_IMSI" = "234159006266939F"
"A_IMEI" = ""
"CALLING_NUM" = "11447810816136"
"CALLED_NUM" = "120414207193"
"CELL_1ST" = "05F54000015DCB"
"CELL_LAST" = "05F54000015DCB"
"TIME_CHANNEL" = "122525"
"FAULT_CODE" = ""
"TELE_CODE" = "11"
"BEARER_CODE" = ""
"CHAN_RATE_INFO" = "01"
"TRANS_IND" = ""
"RF_POWER_A" = "04"
"DTMF" = ""
"SS_CODE" = ""
"CUG_INTERLOCK" = ""
"CUG_INDEX" = ""
"CUG_OUT_ACC_USED" = ""
"CUG_OUT_ACC_IND" = ""
"REG_SERV_USED" = ""
"REG_DEP_CO" = ""
"CHARG_CASE" = "0001"
"DATE" = "020723"
"TIME_CHARGE" = "122531"
"DURATION" = "0005"
"B_CAT" = "00"
"CALL_STAT" = "03"
"ABNRML_REL" = "01"
"TARIFF_CLASS" = "01"
"TARIFF_ACT_CODE" = "01020D"
"A_CAT" = "01"
"OUT_ROUTE" = "535346494E2020"
"EXCHANGE_ID" = "4D4F444D53432F4134382F30305830"
"MSC_ID" = "1161415019900F"
"MISC_INFO" = ""
"ORIG_FOR_CHARGE" = "15"
"CAMEL_SERVER_ADDRESS" = "1144385016450F"
"CAMEL_SERVICE_LEVEL" = "0F"
"CAMEL_SERVICE_KEY" = "00000006"
"CAMEL_CALL_REFERENCE" = "11614150199000005C103E7F"
"CAMEL_DESTINATION" = "120414209999"
"CAMEL_MODIFICATION" = "5F"
"INTERNAL_CAUSE_AND_LOC" = "0003"
End of Record (1)


How to make the awk command recognize "Record (1)" and End of Record (1)"???
The output should display whatever in between.

Part of my script:-

echo "Please enter a field name:"
read FILE1
file1=$1
awk '$1 ~ /'"$FILE1"'/' $DIR > tmp
echo "What do you want to do?"
echo "Do you want to display the contents?"
echo "Please enter y/n only"
read ANS
if test "$ANS" = "y"
then
vi tmp
else
echo "You could see your results in ~/tmp"
echo "Searching is done!!"
echo "Please wait for a few second to return to main menu"
sleep 1
fi
 
Sorry the script should look like this:-

echo "Please enter 2 fields name:"
read FILE1
read FILE2
file1=$1
awk '$1 ~ /'"$FILE1"'/' $DIR > tmp
awk '$1 ~ /'"$FILE2"'/' $DIR >> tmp
echo "Press a to display the the content between the 2 fields entered."
read ANS
if test "$ANS" = "a"
then
echo "Enter a tmp file name besides tmp:"
read TMP
awk '/'"$FILE1"'/, /'"$FILE2"'/' $DIR > $TMP
vi $TMP
else
echo "You could see your results in ~/tmp"
echo "Searching is done!!"
echo "Please wait for a few second to return to main menu"
sleep 1
fi
 
Sed is probably the easier of the two for this and follows
your script model more concisely.
 
Hi marsd,

I'm not familiar with sed commands. Can you please show me how you code this in sed?

Thanks for you help!!
 
example(In line with your script)
read -p "Start pattern: " start
read -p "End pattern: " end
sed -e '/'"$start"'/, /'"$end"'/!d' filename

Good Luck.
 
Thanks marsd.
It works but not perfect.

For example:-
if you enter
start:1
end:4 for sample file below

1
2
3
4
5
6
10
111

The output would come out as
1
2
3
4
10
111
It will search all the pattern with "1"
anyway, thanks for your help.
 
That shouldn't happen.
The sed matching should end after the first "4" encountered.
For it to be useful you should probably
use more sophisticated regexps than 1
and 4.

Ex:
start="^Rec.*"
end="MISC.*"

sed '/'"$start"'/, /'"$end"'/!d' ../scrap.txt
Record (1) "ML_REC"
"REC_SEQ_NUMBER" = "000442"
"CALL_ID" = "00000000"
"REC_NUMBER" = "01"
"CAUSE_FOR_OUTPUT" = "04"
"A_IMSI" = "234159006266939F"
"A_IMEI" = ""
"CALLING_NUM" = "11447810816136"
"CALLED_NUM" = "120414207193"
"CELL_1ST" = "05F54000015DCB"
"CELL_LAST" = "05F54000015DCB"
"TIME_CHANNEL" = "122525"
"FAULT_CODE" = ""
"TELE_CODE" = "11"
"BEARER_CODE" = ""
"CHAN_RATE_INFO" = "01"
"TRANS_IND" = ""
"RF_POWER_A" = "04"
"DTMF" = ""
"SS_CODE" = ""
"CUG_INTERLOCK" = ""
"CUG_INDEX" = ""
"CUG_OUT_ACC_USED" = ""
"CUG_OUT_ACC_IND" = ""
"REG_SERV_USED" = ""
"REG_DEP_CO" = ""
"CHARG_CASE" = "0001"
"DATE" = "020723"
"TIME_CHARGE" = "122531"
"DURATION" = "0005"
"B_CAT" = "00"
"CALL_STAT" = "03"
"ABNRML_REL" = "01"
"TARIFF_CLASS" = "01"
"TARIFF_ACT_CODE" = "01020D"
"A_CAT" = "01"
"OUT_ROUTE" = "535346494E2020"
"EXCHANGE_ID" = "4D4F444D53432F4134382F30305830"
"MSC_ID" = "1161415019900F"
"MISC_INFO" = ""

Good Luck
 
Another related question. More specifix search method.
If you search a field in between Record (1) and End of Record (1)......how do you capture or display the Record #??

Let say u search CALL_ID = 00000000 and DURATION = 0005 field and u want to know which Record that actually contain this fields with those specifix value?
 
Try this:
sed -e '/^Rec.*/, /MISC.*/!d;=' scrap.txt
OP:
2
Record (1) "ML_REC"
3
"REC_SEQ_NUMBER" = "000442"
4
"CALL_ID" = "00000000"
5
"REC_NUMBER" = "01"
6
"CAUSE_FOR_OUTPUT" = "04"
7
"A_IMSI" = "234159006266939F"
8
"A_IMEI" = ""
9
"CALLING_NUM" = "11447810816136"
10
"CALLED_NUM" = "120414207193"
11
"CELL_1ST" = "05F54000015DCB"
12
"CELL_LAST" = "05F54000015DCB"
13
"TIME_CHANNEL" = "122525"
14
"FAULT_CODE" = ""
15
"TELE_CODE" = "11"
16
"BEARER_CODE" = ""
17
"CHAN_RATE_INFO" = "01"
18
"TRANS_IND" = ""
19
"RF_POWER_A" = "04"
20
"DTMF" = ""
21
"SS_CODE" = ""
22
"CUG_INTERLOCK" = ""
23
"CUG_INDEX" = ""
24
"CUG_OUT_ACC_USED" = ""
25
"CUG_OUT_ACC_IND" = ""
26
"REG_SERV_USED" = ""
27
"REG_DEP_CO" = ""
28
"CHARG_CASE" = "0001"
29
"DATE" = "020723"
30
"TIME_CHARGE" = "122531"
31
"DURATION" = "0005"
32
"B_CAT" = "00"
33
"CALL_STAT" = "03"
34
"ABNRML_REL" = "01"
35
"TARIFF_CLASS" = "01"
36
"TARIFF_ACT_CODE" = "01020D"
37
"A_CAT" = "01"
38
"OUT_ROUTE" = "535346494E2020"
39
"EXCHANGE_ID" = "4D4F444D53432F4134382F30305830"
40
"MSC_ID" = "1161415019900F"
41
"MISC_INFO" = ""
 
I'm not quite sure what you're trying to do here.
Are you telling me that i have to modify the data file?
 
No!
This will work with what you have proposed.
sed -e '/'"$start"'/, /'"$end'"/!d;='

This will print your user defined range and the
line number of each read line.

If you need just the end or beginning lines, awk IS
a better bet.
 
Hi marsd,

I got the output.
To make full use of this i need to print the whole data with those numbering.
How do you print the whole content of the data file with sed.
awk uses print $0
Since i'm not familiar with sed, how do you do it. Tried using awk commands but maybe i' doing something wrong.

Thanks again for your help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top