Loop is not reading tabs from the file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Loop is not reading tabs from the file
# 1  
Old 03-13-2012
Loop is not reading tabs from the file

Hi,

I am on HP-UX and K shell.

When I am using while/for loop for reading a file. It is working fine but not reading tabs:

Suppose, if the line is:
Code:
    ;  ;COMP;         ;        ;                                                                ;

then loop is reading as
Code:
; ;COMP; ; ; ; ;

Thanks for replying...
# 2  
Old 03-13-2012
1. Please post your script.
2. What is the exact requirement?
# 3  
Old 03-13-2012
I can post the code fragment:
Code:
      DIR_OK=${TARGET_PATH_OK##*/}
      if grep -Fq "${DIR_OK}" "${PREPAVAL_TRAVAIL}/${EDI_DIRS_list}" && [ -d "${TARGET_PATH_OK}" ]
      then
       FXML_index=`grep ${TARGET_PATH#*_} ${PREPAVAL_TRAVAIL}/${HX_header}`
       counter_src=1
       counter_dest=1
       DIR=${FXML_index%.EDI}
       while read FXML_line
       do
        if [ `echo $FXML_line | awk -F ";" '{print $61;}'` = "S" ] -o [ `echo $FXML_line | awk -F ";" '{print $61;}'` = "H" ]
        then
         if [ -d ${PREPAVAL_TRAVAIL}/${DIR} ]
         then    
          echo ${FXML_line} >> ${PREPAVAL_TRAVAIL}/${DIR}/${FXML_index}
          cp ${TARGET_ROOT_PATH}/${DIR_OK}/f`printf "%0.7d" $counter_src`.xml ${PREPAVAL_TRAVAIL}/${DIR}/f`printf "%0.7d" $counter_dest`.xml 2>/dev/null
 
          counter_dest=$(( $counter_dest+1 ))
 
         else
          mkdir ${PREPAVAL_TRAVAIL}/${DIR} 2>/dev/null
 
          echo ${FXML_line} > ${PREPAVAL_TRAVAIL}/${DIR}/${FXML_index}
          cp ${TARGET_ROOT_PATH}/${DIR_OK}/f`printf "%0.7d" $counter_src`.xml ${PREPAVAL_TRAVAIL}/${DIR}/f`printf "%0.7d" $counter_dest`.xml 2>/dev/null
                    counter_dest=$(( $counter_dest+1 ))
 
         fi
        fi
        counter_src=$(( $counter_src+1 ))
       done < "${PREPAVAL_TRAVAIL}/${FXML_index}"
       mv ${PREPAVAL_TRAVAIL}/${DIR} ${TARGET_EDI} 2>/dev/null
              mv ${PREPAVAL_TRAVAIL}/${FXML_index} ${TARGET_EDI} 2>/dev/null

and the exact requirement is the tabs should be read by the loop, it shouldn't be skipped.

Thanks for replying...

Last edited by ezee; 03-13-2012 at 04:03 AM..
# 4  
Old 03-13-2012
Two issues: To stop read from interpreting the line using read and losing beginning and ending tabs you can use:
Code:
while IFS= read -r FXML_line

to not lose those tabs during variable expansion use
Code:
echo "$FXML_line"

or better yet:
Code:
printf "%s\n" "$FXML_line"

This User Gave Thanks to Scrutinizer For This Post:
# 5  
Old 03-13-2012
Quote:
Originally Posted by Scrutinizer
Code:
while IFS= read -r FXML_line

Small explanation for this line please...
# 6  
Old 03-13-2012
IFS= means set the Input Field Selector to nothing for the read statement, causing no field splitting and keeping leading and ending tabs intact.
-r means do not interpret the \ character.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Sequential Reading from two file in a loop

Hello All, I have two files with me file1.txt and file2.txt file1.txt has: 333 222 111 file2.txt has ccc bbb aaa ccc is related to 333 only, bbb is related to 222 only and aaa is related to 111 only. I have to get the values from each of the file and pass them in the URL... (3 Replies)
Discussion started by: ankur328
3 Replies

2. Shell Programming and Scripting

Removing \r and \n during reading file through while loop

Hi, I am writing in a file through cat command. This file will contain the path of file along with filename. e.g. /home/user/folder1/folder2/filename.txt There might be very large number of this path in same file like say 140 when I try to run while command: while read -r file do //command... (8 Replies)
Discussion started by: Pulkit Lall
8 Replies

3. Shell Programming and Scripting

Need help with perl script with a while loop reading file

Good morning, I appreciate any assistance that I can get from the monks out there. I am able to get this to work for me so that I can do a hostname lookup if I only specify one hostname in the script. What I want to do is have a file with hostnames and do lookups for each name in the file. Here is... (1 Reply)
Discussion started by: brianjb
1 Replies

4. Shell Programming and Scripting

reading the values from a file in C Shell for loop

Hi All, I need small help on for loop syntax in C shell. How can we read the values from a file (line by line) through C shell loop. For Ex: $Cat file1 data1 data2 data3 data4 $ I have to print those values in a variable and have to perform some steps... Can anyone help on... (2 Replies)
Discussion started by: raghu.iv85
2 Replies

5. Shell Programming and Scripting

While loop is not reading next line in the file when IF condition is used.

Hi Guys I am new to scripting.Please forgive for asking basic questions. I want to write a script to check whether the logs are getting updated in last 15 mins. cat server 192.168.1.6 192.168.1.7 cat list 192.168.1.7 /logs/logpath1 192.168.1.7 /logs/logpath2 192.168.1.6... (4 Replies)
Discussion started by: vdurai
4 Replies

6. AIX

How to pause a while loop while reading from a file

Hi, I am building a script to grep for a string in all the files from a folder and display the results. I am reading the files one by one by placing the names in other file using while loop my code is as below while read inp do chk=`grep -c "$str" $pth/$inp` ... (2 Replies)
Discussion started by: sekhar gajjala
2 Replies

7. Shell Programming and Scripting

How to get the modified value of variable outside the while loop reading from a file

Hi Friends , Sorry if this is a repeated question , The input file contains 5 lines , so the the values of the variables i and count should b i=5; count=15 but the variables are not updating , the value of variables showing i=0 and count =0 only.:mad: can any1 help me please. (11 Replies)
Discussion started by: babusek
11 Replies

8. Shell Programming and Scripting

Not access variable outside loop when a reading a file

I am writing a shell script using the korn shell. It seems that I am only able to use local variables within a while loop that is reading a file. (I can't access a variable outside a previously used while loop.) It's been a while since I wrote shell scripts. Here is a sample cat file.txt... (4 Replies)
Discussion started by: ricardo.ludwig
4 Replies

9. UNIX for Dummies Questions & Answers

How to make a loop base on reading a file?

To make it clearer: I have a file, List.txt List.txt contains: (these are actually splitted files w/c I got from ls command and dump them to the List.txt file) SAMPLEa SAMPLEb SAMPLEc SAMPLEd SAMPLEe SAMPLEf . . . . . And I want to rename these files to have a .dat extension.... (3 Replies)
Discussion started by: JohnBalayo
3 Replies

10. HP-UX

How to make a loop base on reading a file?

Need help on making a loop script base on what is inside a file... File to read: List.txt List.txt contains below w/c are file name as well: SAMPLEa SAMPLEb SAMPLEc SAMPLEd SAMPLEe SAMPLEf . . . Want to make a loop that will manipulate those that are inside the file.txt w/c are... (3 Replies)
Discussion started by: JohnBalayo
3 Replies
Login or Register to Ask a Question