Visit Our UNIX and Linux User Community


Breaking strings into Substrings


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Breaking strings into Substrings
# 1  
Old 04-06-2006
Question Breaking strings into Substrings

I'm only new to shell programming and have been given a task to do a program in .sh, however I've come to a point where I'm not sure what to do. This is my code so far:
# process all arguments (i.e. loop while $1 is present)
while [ -n "$1" ] ; do
# echo "Arg is $1"

case $1 in
-h*|-H*) echo "help msg1" ; echo "help msg2" ;
shift ;;

[12][0-9][0-9][0-9]) the_year=$1 ;
shift ;;

Jan*|jan*|JAN*) the_month="Jan" ; shift ;;
Feb*|feb*|FEB*) the_month="Feb" ; shift ;;
Mar*|mar*|MAR*) the_month="Mar" ; shift ;;
Apr*|apr*|APR*) the_month="Apr" ; shift ;;
May*|may*|MAY*) the_month="May" ; shift ;;
Jun*|jun*|JUN*) the_month="Jun" ; shift ;;
Jul*|jul*|JUL*) the_month="Jul" ; shift ;;
Aug*|aug*|AUG*) the_month="Aug" ; shift ;;
Sep*|sep*|SEP*) the_month="Sep" ; shift ;;
Oct*|oct*|OCT*) the_month="Oct" ; shift ;;
Nov*|nov*|NOV*) the_month="Nov" ; shift ;;
Dec*|dec*|DEC*) the_month="Dec" ; shift ;;

*) echo "? unrecognised input: $1" ;
shift ;;
esac
done

echo "Got month: $the_month"
echo "Got year: $the_year"

oldIFS="$IFS"
count=0
IFS="/"; while read file
do
set -- $file
IFS=":"
set -- $1

if [ "$3" = "$the_year" ]
then
echo $file
count=`expr $count + 1`
else
if [ "$2" = "$the_month" ]
then
echo $file
count=`expr $count + 1`
fi
fi
done < my_sample_log2.txt
echo $count
IFS=$oldIFS
What my problem is where the IFS changes happen. I need to break the text into substrings based on field separators of "/" - only interested in the month and year patterns. Also need a counter/s which are updated according to the substrings found in the input data.

My program isn't working, and I have a feeling it is to do with two IFS changes, but I don't know how else to break the string up to just get the Month or year. The lines in the file look something like:
66.196.90.230 - - [01/Mar/2006:01:28:59 +1000] "GET /file.txt HTTP/1.0" 404 282

Thanks.
# 2  
Old 04-06-2006
Quote:
Originally Posted by switch
have been given a task to do a program in .sh,
Is it homework ? Homework posts are not allowed in this forum.

Since you have quite a bit, here is a little modification that you could use.

Code:
IFS="/"; while read file
do
set -- $file
IFS=":"
set -- $1

will become

Code:
while IFS="/" read day month year rest
do
MONTH=$month
YEAR=$year

Modify the rest accordingly.
# 3  
Old 04-06-2006
No, it isn't homework. Just something I'm trying to do to try and extend learning of shell, which isn't working very well.

Thank you for that input, however I am still having the same main issue. I can't extract the year from that sequence with just changing the IFS to /, as the year has 2006:01:28:59 +1000] "GET due to that being the next argument till the next /. That is why I was attempting to then change the IFS to a : to get just the 2006 from that substring, thus it never steps into the year if statement.

Thanks again.
# 4  
Old 04-06-2006
Include : as a delimiter too.

Code:
while IFS="/:" read day month year rest
do
MONTH=$month
YEAR=$year

# 5  
Old 04-06-2006
Haha. Feel so stupid now, I didn't realise you could include two delimiters in the one IFS statement. Doh. But that now works, thanks heaps and now I can do some further progress!

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Look for substrings with special characters

Hello gurus, I have a lookup table cat tmp1 \\\erw``~ 1 ^774574574565665f\] 2 ()42543^ and I`m trying to compare a bunch of strings such that, either the lookup table column 1, or the string to be looked up are substrings of each other (and return the second lookup column if yes). ... (2 Replies)
Discussion started by: sheetalk
2 Replies

2. Shell Programming and Scripting

Finding most common substrings

Hello, I would like to know what is the three most abundant substrings of length 6 from col2. The file is quite large and looks like this col1 col2 EN03 typehellobyedogcatcatdog EN09 typehellobyebyebyebye EN08 dogcatcatdogbyebyebyebye EN09 catcattypehellobyebyebyebye... (9 Replies)
Discussion started by: verse123
9 Replies

3. UNIX for Dummies Questions & Answers

Replace substrings in awk

Hi ! my input looks like that: --AAA-AAAAAAA---------AA- AAA------AAAAAAAAAAAAAA ------A----AAAA-----A------- Using awk, I would need to replace only the "-" located between the last letter and the end of the string by "~" in order to get: --AAA-AAAAAAA---------AA~... (7 Replies)
Discussion started by: beca123456
7 Replies

4. Shell Programming and Scripting

Comparing multiple substrings for a match

I have a tab-delimited file containing a large genetic dataset with binary base calls, in this format: Chr7 26021407 1/1:0,0,0:5 1/1:0,0,0:5 1/1:0,0,0:5 Chr7 26022023 1/1:0,0,0:3 1/1:0,0,0:3 1/1:28,3,0:5 Chr7 26022087 1/1:0,0,0:6 1/1:25,3,0:9 1/1:25,3,0:9 Chr7 26022656 1/1:0,0,0:3... (1 Reply)
Discussion started by: ljk
1 Replies

5. Shell Programming and Scripting

Extract three substrings from a logfile

I have a log file like below. 66.249.73.11 - - "UCiZ7QocVqYAABgwfP8AAHAA" "US" "Mediapartners-Google" "-" www.mahashwetha.com.sg "GET... (2 Replies)
Discussion started by: Tuxidow
2 Replies

6. Shell Programming and Scripting

extracting substrings from variables

Hello Everyone, I am looking for a way to extract substrings to local variables. Here is the format of the string variable i am using : /var/x/www && /usr/x/share/doc && /etc/x/logs where the substrings i must extract are the "/var/x/www" and such. I was originally thinking of using... (15 Replies)
Discussion started by: jimmy75_13
15 Replies

7. Shell Programming and Scripting

extracting substrings

Hi guys, I am stuck in this problem. Please help. I have two files. FILE1 (with records starting from '>' ) >TC1723_3 similar to Scific_A7Q9Q3 EMSPSQDYCDDYFKLTYPCTAGAQYYGRGALPVYWNYNYGAIGEALKLDLLNHPEYIEQN ATMAFQAAIWRWMNPMKKGQPSAHDAFVGNWKP >TC214_2 similar to Quiet_Ref100_Q8W2B2 Cluster;... (1 Reply)
Discussion started by: smriti_shridhar
1 Replies

8. AIX

Substrings and the likes in AIX 4.2 ?

In AIX 4.2, are there any shell commands to do substrings and the text like manipulation commands ? I want to take an error log where errors are multi-ligned and convert them into single lines to ease tracking/monitoring. I may need to shorten them out too. If I can manage to put them into an... (2 Replies)
Discussion started by: Browser_ice
2 Replies

9. Shell Programming and Scripting

Extract large list of substrings

I have a very long string (millions of characters). I have a file with start location and length that is thousands of rows long: Start Length 5 10 16 21 44 100 215 37 ... I'd like to extract the substring that corresponds to the start and length from each row of the list: I tried... (7 Replies)
Discussion started by: dcfargo
7 Replies

10. Shell Programming and Scripting

ksh: Comparing strings that contain spaces and working with substrings

Forgive me. I am very new to kornshell scripts. The simplest things stop me dead in my tracks. Here are two such examples. I want to save the first 19 characters of the following string to a variable. "Operation Completed and blah blah blah" I know this works (from another thread): ... (2 Replies)
Discussion started by: nancylt723
2 Replies

Featured Tech Videos