FYI: Change blank to Tab but keep spaces between quotation marks

FYI: Change blank to Tab but keep spaces between quotation marks

Post by Hajo Ehler » Fri, 11 May 2001 16:39:27



May sameone has use for it
Hajo

ChangeToTabDelimited () {
# Call  ChangeToTabDelimited 'A file with 2 spaces in its filename:
"/tmp/x  x"'
# Unnessary spaces are removed.
set -x
typeset Line="$1"
typeset Parameter=""
typeset -i MarkCount=0
typeset -i Flag=0
typeset -i SwapFlag=0

# <A "text  with spaces" .   "What?"> --> <A[TAB]"text  with
spaces"[TAB].[TAB]"This">
#Text           Count   Flag    Parameter
#A              0       0       <A>
#"text          1       0->1    <"text >
#               0       1       <"text  >
#with           0       1       <"text  with >
#spaces"        1       1->0    <"text  with space" >
#.              0       0       <.
#               0       0       <DELETED LINE>
#               0       0       <DELETED LINE>
#"What?"         2       0      <"What?">

# REMARK : print or echo will reduce white spaces in a string to one.
#          A="a   b"; print $A  : "a b"
#          To keep all spaces you must enclose the variable in quotation
marks
#          A="a   b"; print "$A"  : "a   b"

# Parse $Line
{ print "$Line" | sed 's/^[[:blank:]]*//' | tr " " "\\n" |\
while read Line ; do
  # Get the amount of quotation marks from $Line
  MarkCount=$( echo "$Line" | awk '{ print ( gsub("\"", "\"" )) }' ) #
Amount of quotation marks ( " )
  if [[ $MarkCount  -eq  1 ]] ; then  # Switch Flag
    [[ $Flag -eq 1 ]] && SwapFlag=0
    [[ $Flag -eq 0 ]] && SwapFlag=1
    Flag=$SwapFlag
  fi

  # Create $Parameter
  if [[ $Flag -eq 1 || $MarkCount -eq 1 ]] ; then
     Parameter="$Parameter""$Line"" " # Do not remove the " " at the of
the line
  else  # Both Values are Zero
     Parameter="$Line"
  fi

  # Replace CR with TAB , remove unnessary "spaces" and create Output
  if [[ $Flag -eq 0 && ! $Line  = "" ]]  ; then
    Parameter=$(echo "$Parameter" | sed 's/ *$//') # Removes trailing
spaces
    print "$Parameter" | tr "\n" "\t"
    Parameter=""
  fi
done ; } | read Output

# Print Output
Output=$( print "$Output" |sed 's/^[[:blank:]]//;s/[[:blank:]]$//' ) #
Remove leading and trailing [[:blank:]]
print "$Output"
return 0

Quote:}

 
 
 

1. Keeping beginning tabs & spaces in script

Hi.

    I have the following script that display a file line-by-line.

cat file2 | while read LINE
do
 echo $LINE
done

    However I discovered that all beginning tabs and spaces are lost during
this execution.  How to keep the line read as it is without losing the tabs
and spaces in each line.

Thanks in advance for any help provided.

Regards

2. Newbie Question - Global .login?

3. the quotation mark "

4. Q:>I want to know process that locked port

5. Getting Environment Variables from CGI C++ Program

6. Quotation marks in KDE

7. Problems with RedHat 4.1 Install

8. quotation marks in TeX-mode

9. VI - Quotation Marks

10. quotation marks in TeX-mode

11. Star Office : How do I get English quotation marks?

12. why quotation mark lost when using $@ to show bash parameters?