...yet another string of awk/sed questions from a RegExp-Challenged luser %-\


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting ...yet another string of awk/sed questions from a RegExp-Challenged luser %-\
# 8  
Old 10-02-2009
Glad you like it Steve!
Thanks for your comments CFA.
# 9  
Old 10-02-2009
Bug ...a possibly trivial, follow-up question.

...a possibly trivial, follow-up question.

Is it possible to treat several characters as a single option within a character set?

For instance, some of my routing table entries begin with "D EX" or "S*"

Is it possible to somehow include these strings along with the individual characters in the set [DCS] (and obviously match on and move these strings to the end end of their respective line before evaluating the individual characters) or would I need something like a separate invocation of SED to do this?

Thanks!
--Steve
# 10  
Old 10-02-2009

Look at the -e option in my last post in this thread.

(And read the man page.)
# 11  
Old 10-02-2009
...got it.

Thanks CFA.

--Steve

---------- Post updated at 05:48 PM ---------- Previous update was at 05:41 PM ----------

I imagine this is just being caused by a minor typo or something but I'm getting the following error when I run the script:

Code:
 
$ ./CleanRouteTable.sh < GNWAN-RT
./CleanRouteTable.sh: line 8: syntax error near unexpected token `&'
./CleanRouteTable.sh: line 8: ` echo; &'

BTW, don't hate me for this, but I'm currently trying to run this using Cygwin Smilie

...so maybe that's also a problem here... Smilie


--Steve
# 12  
Old 10-02-2009
Quote:
Originally Posted by SteveB-in-LV
I imagine this is just being caused by a minor typo or something but I'm getting the following error when I run the script:

Code:
 
$ ./CleanRouteTable.sh < GNWAN-RT
./CleanRouteTable.sh: line 8: syntax error near unexpected token `&'
./CleanRouteTable.sh: line 8: ` echo; &'


Did you read the error message?
Quote:
BTW, don't hate me for this, but I'm currently trying to run this using Cygwin Smilie

...so maybe that's also a problem here... Smilie

It's a problem, but that's not relevant in this instance.
# 13  
Old 10-02-2009
The problem is with the ;& body termination, which is used for "falling through" in case statements. I guess this is a Kornshell-only feature.

I have adjusted the code somewhat so that it works in both ksh and bash:
Code:
#!/bin/ksh
# First Step 2
mergedline=0
{ sed 's/^\([CDS]\) \(.*\)$/\2 \1/' infile |\
while read line; do
  case $line in
      # Step 3
      \[*) printf "$prevline $line\n"
           mergedline=1
           prevline="" ;;
      # Step 4
      *)   if [[ $mergedline -eq 0 ]]; then
             echo $prevline
      # Step 1
             case $line in
               *subnets*|*masks*) echo ;;
             esac
           fi
           mergedline=0
           prevline=$line  ;;
  esac;
done;
if [[ $mergedline -eq 1 ]]; then
  echo $prevline
fi } | \
while read firstline; do  # Step 5
  while read line; do
    if [[ $firstline != "" ]]; then
      echo $firstline
      firstline=""
    fi
    if [[ $line != "" ]]; then
      echo $line
    else
      break
    fi
  done|sort -n -t. -k1,1 -k2,2 -k3,3 -k4,4
  echo
done
if [[ $firstline != "" ]]; then
  echo $firstline
fi



---------- Post updated at 05:59 PM ---------- Previous update was at 05:36 PM ----------

And this version works in sh/dash too:

Code:
#!/bin/sh
# First Step 2
mergedline=0
{ sed 's/^\([CDS]\) \(.*\)$/\2 \1/' infile |\
while read line; do
  case $line in
      # Step 3
      \[*) printf "$prevline $line\n"
           mergedline=1
           prevline="" ;;
      # Step 4
      *)   if [ $mergedline -eq 0 ]; then
             echo $prevline
      # Step 1
             case $line in
               *subnets*|*masks*) echo ;;
             esac
           fi
           mergedline=0
           prevline=$line  ;;
  esac;
done;
if [ $mergedline -eq 1 ]; then
  echo $prevline
fi } | \
while read firstline; do  # Step 5
  while read line; do
    if [ -n "$firstline" ]; then
      echo $firstline
      firstline=""
    fi
    if [ -n "$line" ]; then
      echo $line
    else
      break
    fi
  done|sort -n -t. -k1,1 -k2,2 -k3,3 -k4,4
  echo
done
if [ -n "$firstline" ]; then
  echo $firstline
fi

# 14  
Old 10-02-2009
Hello again, Scrutinizer.

...I'll apologize in advance for having defaced your code; My script-foo is no match for yours so I've dumbed it down a bit. Smilie

...what can I say...I've got a lot to learn.

...anyway, below is what I've got right now. This runs fairly well but stumbles at at least one point on our live routing tables.

Sometimes there are multiple equal-cost paths to a particular network so what this means in terms of processing the routing table with this script is, there may be several lines in a row which begin with a "[" and need to be joined to the line above them iteratively.

I imagine this means I need something like a WHILE loop for Step 3. to ensure I've joined all the "[" lines before moving on.

How would you suggest I proceed?

Code:
 
#!/bin/bash
# First Step 2
mergedline=0
{ sed '/^D EX/s/$/\tD EX/;s/^D EX//; 
         /^D/s/$/\tD/;s/^D//; 
         /^C/s/$/\tC/;s/^C//; 
         /^S\*/s/$/\tS\*/;s/^S\*//; 
         /^S/s/$/\tS/;s/^S//; 
         s/^[ \t]*//; 
         /masks/s/^/\n/; 
         /subnets/s/^/\n/' $1 |\
while read line; do
  case $line in
      # Step 3
      \[*) printf "$prevline $line\n"
           mergedline=1
           prevline="" ;;
      # Step 4
      *)   if [[ $mergedline -eq 0 ]]; then
             echo $prevline
      # Step 1
             case $line in
               *subnets*|*masks*) echo ;;
             esac
           fi
           mergedline=0
           prevline=$line  ;;
  esac;
done;
if [[ $mergedline -eq 1 ]]; then
  echo $prevline
fi } | \
while read firstline; do  # Step 5
  while read line; do
    if [ -n "$firstline" ]; then
      echo $firstline
      firstline=""
    fi
    if [ -n "$line" ]; then
      echo $line
    else
      break
    fi
  done|sort -n -t. -k1,1 -k2,2 -k3,3 -k4,4
  echo
done
if  [ -n "$firstline" ]; then
  echo $firstline
fi

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

awk or sed to print the character from the previous line after the regexp match

Hi All, I need to print the characters in the previous line just before the regular expression match Please have a look at the input file as attached I need to match the regular expression ^ with the character of the previous like and also the pin numbers and the output file should be like... (6 Replies)
Discussion started by: kshitij
6 Replies

2. Shell Programming and Scripting

Replace string in XML file with awk/sed with string from another

Sorry for the long/weird title but I'm stuck on a problem I have. I have this XML file: </member> <member> <name>TransactionID</name> <value><string>123456789123456</string></value> </member> <member> <name>Number</name> ... (9 Replies)
Discussion started by: cozzin
9 Replies

3. Shell Programming and Scripting

Regexp for string that might contain a given character

I'm probably just not thinking of the correct term to search for :-) But I want to match a pattern that might be 'ABC' or '1ABC' there might be three characters, or there might be four, but if there are four, the first has to be 1 (1 Reply)
Discussion started by: jnojr
1 Replies

4. Shell Programming and Scripting

Couple of easy questions for experts on awk/sed

Hello Experts.. I have 3-4 C codes with Oracle SQL statements embedded. All the SQL statements starts with EXEC SQL keyword and ends with ;. I want to extract all the SQL statements out of these codes. I did awk '/^EXEC SQL/,/\;/' inputFile (I use this on all of the codes individually). That... (2 Replies)
Discussion started by: juzz4fun
2 Replies

5. Shell Programming and Scripting

sed or awk command to replace a string pattern with another string based on position of this string

here is what i want to achieve... consider a file contains below contents. the file size is large about 60mb cat dump.sql INSERT INTO `table1` (`id`, `action`, `date`, `descrip`, `lastModified`) VALUES (1,'Change','2011-05-05 00:00:00','Account Updated','2012-02-10... (10 Replies)
Discussion started by: vivek d r
10 Replies

6. Shell Programming and Scripting

awk, sed or perl regexp to print values from file

Hello all According to the following file (orignal one contains 200x times the same structure...) I was wondering if someone could help me to print <byte>??</byte> values example, running this script/command like ./script.sh xxapp I would expect as output: 102 116 112 ./script.sh xxapp2... (2 Replies)
Discussion started by: cabrao
2 Replies

7. UNIX for Dummies Questions & Answers

sed before and after regexp

Dear all i have the code which print 1 line of context before and after regexp, with line number sed -n -e '/regexp/{=;x;1!p;g;$!N;p;D;}' -e h the code work well but any one can tell me what each letter mean {=;x;1!p;g;$!N;p;D;} also how i can print 2 line before and onle line after ... (2 Replies)
Discussion started by: soly
2 Replies

8. Shell Programming and Scripting

sed regexp

Hi, I am not that good with reg exp and sed. But I was just looking at something the other day and came across a situation. When I ran the below command: echo "123 word" | sed 's/*/(&)/' the op was: (123) word But when I ran: echo "123 word" | sed 's/*/(&)/g' the o/p was: (123)... (4 Replies)
Discussion started by: King Nothing
4 Replies

9. Shell Programming and Scripting

regexp to get first line of string

Hi everybody for file in * #Bash performs filename expansion #+ on expressions that globbing recognizes. do output="`grep -n "$1" "$file"`" echo "$file: `expr "$output" : '\(^.*$\)'`" done In the above bash script segment, I try to print just the first line of string named... (3 Replies)
Discussion started by: jonas.gabriel
3 Replies

10. Shell Programming and Scripting

regexp with sed again!!!

please help: I want to add 1 space between string and numbers: input file: abcd12345 output file: abcd 1234 The following sed command does not work: sed 's/\(+\)\(+\)/\1 \2/' file Any ideas, please Andy (2 Replies)
Discussion started by: andy2000
2 Replies
Login or Register to Ask a Question