Need a better understanding of shell scripts

Need a better understanding of shell scripts

if sed or awk work, why use another way? is this homework?

its not a homework. I am learning shell scripting and i am trying to understand the different possibilities to solve a particular problem..

sed looks like a single line command, with which i can acheive the above said result.
but i am trying to get a multi line script to under shell better...

Something working with your example (file1 contains the atom data)

#!/bin/bash
for TXT in $(grep -o 'ftp://.*' file1)
do	TXT=${TXT%/\"/>*};	echo "$TXT"
done

Without using sed or awk, cut might be your next option. Try this:

grep "ftp://" logfile | cut -d'"' -f4

This will first look for any line containing "ftp://", then extract the 4th field delimited by ". This only works if all the ftp addresses occur in this form. Hope it helps.

Thanks frans & 2pugs...
Your reply was really helpful...

In that case, this might work for you. Basically it will look line by line in your log file and look for atom start/end entries. It uses a "switch" variable called "recording" that turns ON and OFF based on if it found an entry. Let me know if any of it is unclear. Hope it helps.

# Initialize recording to "OFF"
recording=OFF
while read line
do
  if [ "${line}" = "^<atom:entry>" ]
  then
    # Atom entry start, switch recording to "ON"
    recording=ON
  fi

  if [ "${recording}" = "ON" ]
  then
    # If we are recording, record entry to outfile
    echo ${line} >> outfile
  fi


  if [ "${line}" = "^</atom:entry>" ]
  then
    # End of atom entry, switch recording to "OFF"
    recording=OFF
  fi
done < inputfile

Thanks for your immediate response..

i tried your above code.. but its not working..

its not throwing any error. But outfile is not created. Hope u can help me on this..

Can you post the error you're getting? It might be a syntax error I missed.

Thanks for your reply..

I tried the above code.. but outfile is not created.. i cant see any error message too..

Sorry. I misread and thought there was an error. I added a few lines just for debugging. This should output to the script what it's find. Let me know if this works.

rm outfile  # Start with a new outfile each time
# Initialize recording to "OFF"
recording=OFF
while read line
do
  if [ "${line}" = "^<atom:entry>" ]
  then
    # Atom entry start, switch recording to "ON"
    recording=ON
    echo "DEBUG: Switching recording to ON" ; sleep 2
  fi

  if [ "${recording}" = "ON" ]
  then
    # If we are recording, record entry to outfile
    echo ${line} >> outfile
  fi
  echo "DEBUG: \$recording=$recording \$line=$line"


  if [ "${line}" = "^</atom:entry>" ]
  then
    # End of atom entry, switch recording to "OFF"
    recording=OFF
    echo "DEBUG: Switching recording to OFF" ; sleep 2
  fi
done < inputfile

if [ -f outfile ]
then
  echo "DEBUG: outfile created"
  ls -l outfile
else
  echo "DEBUG: outfile NOT created"
fi

Thanks for your support..

its debugging and showing the message as

....
......
.....
DEBUG: $recording=OFF $line=<mitt:group>100</mitt:group>
DEBUG: $recording=OFF $line=</atom:entry>
DEBUG: $recording=OFF $line=</atom:feed>
DEBUG: outfile NOT created

It looks like it's not finding the start and end atom entries so it is never switching it ON. My first guess is maybe it's the "^" I added to tell it's at the beginning of the line. Let's try removing it for both start and end. So instead of

  if [ "${line}" = "^<atom:entry>" ]
.
.
.
  if [ "${line}" = "^</atom:entry>" ]

You would have:

  if [ "${line}" = "<atom:entry>" ]
.
.
.
  if [ "${line}" = "</atom:entry>" ]

Thanks a lot.. that really works...

i am trying to understand each and every line... it will be helpful in my future scripts.. thanku..

Glad it helps. Let me know if there are parts to the code that is unclear. I'd be happy to explain.