File Processing: Handling spaces in a line


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users File Processing: Handling spaces in a line
# 8  
Old 11-15-2013
Would this come close to what you want to achieve:
Code:
while IFS="=" read A B
  do case $A in
       "#"*) printf "comment" ;;
         "") printf "empty" ;;
          *) read $A < <(echo $B) ;;
     esac
  eval echo "\$A="\$$A
  done <file

? That eval echo is just for controlling purposes.
This User Gave Thanks to RudiC For This Post:
# 9  
Old 11-15-2013
Can we see a sample input file and which ones you want to use and which to exclude?

Please past in CODE tags to make it easier to read.



Robin
# 10  
Old 11-15-2013
I have already posted sample input with which ones I want to see, please see my earlier post

I want to remove only empty lines and lines starting with #
Also line with the substring WHOA

where
Code:
WHOA= %s : %S - %C [%c xxxx]

I want to avoid
# 11  
Old 11-15-2013
Just include a line like WHOA) ;; above to eliminate that variable...
# 12  
Old 11-15-2013
Yes, I have seen that you were editing your post as I was writing mine. Oh well Smilie

So, if you want to exclude all blank lines, comments (actually starting with a #) and those starting "WHOA=", then you can have all this in one grep thus:-
Code:
egrep -v "^$|^#|^WHOA=" file > tmp/myenv.$$
. /tmp/myenv.$$
rm /tmp/myenv.$$



You have various suggestions now. Which one is closest to what you need? We can concentrate on that route.



Robin
This User Gave Thanks to rbatte1 For This Post:
# 13  
Old 11-15-2013
I think I want to join if possible the below solution

Code:
while IFS="=" read VAR VALUE
do
        [ -z "$VAR" ] && continue # Ignore blank lines
        case "$VAR" in
        WHOA*)  ;;

        *)
                read $VAR <<EOF
$VALUE
EOF
                export $VAR
                ;;
        esac
done < $file

with yours
Code:
egrep -v "^$|^#|^WHOA=" file > tmp/myenv.$$
. /tmp/myenv.$$   -- This I dod not need as I only use these variables in another shell script to set the environment 
rm /tmp/myenv.$$

# 14  
Old 11-15-2013
Quote:
Originally Posted by Corona688
for x in `cat something` is basically always wrong because of the problem you have discovered among others. You could be doing a while read line ; do ... done < inputfile loop.

If your input file is of the form VAR=VALUE, you can do this:

Code:
while IFS="=" read VAR VALUE
do
        [ -z "$VAR" ] && continue # Ignore blank lines
        case "$VAR" in
        WHOA*)  ;;

        *)
                read $VAR <<EOF
$VALUE
EOF
                export $VAR
                ;;
        esac
done < $file

Which means that, if someone puts `rm -Rf ~/` in your configuration file, it will store that strong instead of running that string like eval would!
@Robin:
the here document is a trick to feed variables to read in a safe manner. The here document is pre-processed by the shell (and usually stored in a temp file) then opened as a stream. While processing the here document a `command` is executed and a $var is substituted, but not the contents of $var.
BTW the processing can be restricted by putting the EOF marker in quotes: <<"EOF" or <<'EOF', then the here document is treated as a quoted string.

I am not convinced that one cannot use the evil eval here.
Code:
while IFS="=" read VAR VALUE
do
  case "$VAR" in
    WHOA*)
    ;;
    [A-Z]*)
    eval $VAR='$VALUE'
    export $VAR
    ;;
  esac
done < $file


Last edited by MadeInGermany; 11-15-2013 at 02:08 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Handling filenames with spaces

I'm trying to handle some files with spaces in their name using "" or \ . Like "file 1" or file\ 1. My current confusion can be expressed by the following shell script: #!/bin/bash touch "file 1" "file 2" echo -n "ls: " ; ls echo --- for file in "file 1" "file 2" ; do echo $file... (9 Replies)
Discussion started by: Ralph
9 Replies

2. Shell Programming and Scripting

[awk] line by line processing the same file

Hey, not too good at this, so I only managed a clumsy and SLOW solution to my problem that needs a drastic speed up. Any ideas how I write the following in awk only? Code is supposed to do... For every line read column values $6, $7, $8 and do a calculation with the same column values of every... (6 Replies)
Discussion started by: origamisven
6 Replies

3. Shell Programming and Scripting

BASH - Handling background processes - distributed processing

NOTE: I am using BASH and Solaris 10 for this. Currently in the process of building a script that has a main "watcher" daemon that reads a configuration file and starts background processes based on it's global configuration. It is basically an infinite loop of configuration reading. Some of the... (4 Replies)
Discussion started by: dcarrion87
4 Replies

4. Shell Programming and Scripting

AWK: Remove spaces before processing each line?

Hi, all I have a file containing the following data: name: PRODUCT_1 date: 2010-01-07 really_long_name: PRODUCT_ABCDEFG I want to get the date (it is "2010-01-07" here), I could use the following code to do that: awk... (6 Replies)
Discussion started by: kevintse
6 Replies

5. Shell Programming and Scripting

Handling directory with spaces in for loops

Hi everyone, I have been a big fan here since a couple years (since I started being an admin ...) and finally decided to become a member and help ppl and perhaps being helped Now I have a problem that might interest some of the gurus. I am abig fan of what I call "one liners". I am trying... (2 Replies)
Discussion started by: plmachiavel
2 Replies

6. UNIX for Dummies Questions & Answers

handling white spaces with getopt

Hi I'm trying to ensure that I have catered for all situations with my getopt cases. One other situation I want to cover is should the user enter the script without any preceding arguments eg: ./script_eg I need the script to the direct the user to the helpfile I have tried... (3 Replies)
Discussion started by: ladyAnne
3 Replies

7. Shell Programming and Scripting

Handling blank spaces

Hi, I am trying to replace a specific column values in a csv file with double quotes when I am find embedded spaces with in the fields. Example: SNO,NAME,ZIPCODE,RANK,SEX,ADDRESS 1,Robert,74538,12,34, M,Robert Street, NY 2,Sam,07564,13,M,12 Main Ave, CA 3,Kim, Ed,12345,14,M,123D ,... (1 Reply)
Discussion started by: techmoris
1 Replies

8. Shell Programming and Scripting

reading a file inside awk and processing line by line

Hi Sorry to multipost. I am opening the new thread because the earlier threads head was misleading to my current doubt. and i am stuck. list=`cat /u/Test/programs`; psg "ServTest" | awk -v listawk=$list '{ cmd_name=($5 ~ /^/)? $9:$8 for(pgmname in listawk) ... (6 Replies)
Discussion started by: Anteus
6 Replies

9. Shell Programming and Scripting

Reading a file line by line and processing for each line

Hi, I am a beginner in shell scripting. I have written the following script, which is supposed to process the while loop for each line in the sid_home.txt file. But I'm getting the 'end of file' unexpected for the last line. The file sid_home.txt gets generated as expected, but the script... (6 Replies)
Discussion started by: sagarparadkar
6 Replies

10. Shell Programming and Scripting

handling spaces in unix

I am testing a ksh script for email. In the script I receive several parameters. One of them is a subject. The subject may contain spaces. Ex. Test this. When I am running the script on telnet to test, how should the syntax at the command line be written. I have this: ksh ResendE.sh '001111'... (2 Replies)
Discussion started by: supercbw
2 Replies
Login or Register to Ask a Question