Cut and WhiteSpace Problem


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Cut and WhiteSpace Problem
# 8  
Old 09-13-2010
I have confirmed that white space is the problem. If I take the ws out the 5th fields are placed correctly into the variable.

The other values are also read correctly

so

v1=N
v2=N
v3=N
v4=N
SUBJECT=Thisisthefirstone


and the second time


v1=N
v2=N
v3=N
v4=N
SUBJECT=Thisisthefirstone


But take put the white space back again and you get


v1=N
v2=N
v3=N
v4=N
SUBJECT=This


and the second time


v1=is
v2=is
v3=is
v4=is
SUBJECT=is


This occurs with

SUBJECT=`echo "$PROCESS_RECORD" | cut -d "|" -f5`

and

SUBJECT=`echo "$PROCESS_RECORD" | cut -d "|" -f5-`
# 9  
Old 09-13-2010
Check if this works -

Code:
grep -i "^${PRCSNAME}" ${PARAMETER_OUTBOUND} | while read PROCESS_RECORD; do
  SUBJECT="`echo ${PROCESS_RECORD} | cut -d '|' -f5-`"
  echo $SUBJECT
done

tyler_durden
# 10  
Old 09-13-2010
Post your script, some real lines of your file and the expected output, if none of the solutions are working for you.
# 11  
Old 09-14-2010
Quote:
Originally Posted by Franklin52
Post your script, some real lines of your file and the expected output, if none of the solutions are working for you.
Hi it's pretty much as explained, but for clarity

KSH Script

Quote:

PARAMETER_OUTBOUND=File1.txt
for PROCESS_RECORD in `grep -i "^H1" ${PARAMETER_OUTBOUND}` ; do

VAR1=`echo ${PROCESS_RECORD} | cut -d "|" -f2`
SUBJECT=`echo ${PROCESS_RECORD} | cut -d "|" -f3`

done
Input text file - file1.txt

Quote:
H1|Y|This is the First Line
H1|N|This is the Second Line
The expected result is to iterate over the two lines

Quote:
for the first iteration

VAR1 =Y
SUBJECT=This is the first line

for the second iteration

VAR1=N
SUBJECT=This is the second line
The Actual Results are

Quote:
for the first iteration

VAR1 =Y
SUBJECT=This

for the second iteration

VAR1=is
SUBJECT=is
I'm grateful for all the suggestions so far, but unfortunately they either reproduce my current incorrect results or results in the SUBJECT not being assigned a value. It could of course be because I'm not using the suggestion correctly.
# 12  
Old 09-14-2010
OK, try this:
Code:
PARAMETER_OUTBOUND=File1.txt

grep -i "^H1" ${PARAMETER_OUTBOUND} | while read PROCESS_RECORD ; do
  VAR1=`echo ${PROCESS_RECORD} | cut -d "|" -f2`
  SUBJECT=`echo ${PROCESS_RECORD} | cut -d "|" -f3`

  echo "$VAR1"
  echo "$SUBJECT"
done

This User Gave Thanks to Franklin52 For This Post:
# 13  
Old 09-14-2010
I agree with Franklin52 and other posters. The construct with "for" is not useful in this context.

Similar inversion of the script but with more quotes in case we get more than one consecutive space character.

Code:
PARAMETER_OUTBOUND=File1.txt

grep -i "^H1" ${PARAMETER_OUTBOUND} | while read PROCESS_RECORD
do
        VAR1=`echo "${PROCESS_RECORD}" | cut -d "|" -f2`
        SUBJECT=`echo "${PROCESS_RECORD}" | cut -d "|" -f3`

        echo "VAR1=${VAR1}"
        echo "SUBJECT=${SUBJECT}"
done

VAR1=Y
SUBJECT=This is the First Line
VAR1=N
SUBJECT=This is the Second Line

# 14  
Old 09-14-2010
Quote:
Originally Posted by Franklin52
OK, try this:
Code:
PARAMETER_OUTBOUND=File1.txt

grep -i "^H1" ${PARAMETER_OUTBOUND} | while read PROCESS_RECORD ; do
  VAR1=`echo ${PROCESS_RECORD} | cut -d "|" -f2`
  SUBJECT=`echo ${PROCESS_RECORD} | cut -d "|" -f3`

  echo "$VAR1"
  echo "$SUBJECT"
done

This worked for the first line of the file but doesn't read the second line

EDIT: Because the grep returns both lines as one line concatenated. It only thinks there is one line


EDIT: Works when you put | at the end of each line in the config file


Many thanks to everyone

Last edited by Greygor; 09-14-2010 at 07:27 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Problem in Cut and paste

Hi, I have a file like this --> Consider z as space #cat filename ABC <!--Nzzzzz--> <!--RESUMO--> EFG XYZ <!--Nzzzzz--> <!--RESUMO--> I need to cut the <!--RESUMO--> part and paste it to the previous line so that the file will look like this--> ABC <!--Nzzzzz--><!--RESUMO-->... (4 Replies)
Discussion started by: samsonata
4 Replies

2. Shell Programming and Scripting

Problem with cut and wc

Hi, I am facing issue with cut and wc. here is the sample. the data in file - tail -1 05_19_BT_TBL_LOAD_20120524064242.bad|cut -c9-58 WatsSaver - AGGREGATED PLAN1581 CALLS FOR 2872.6 tail -1 05_19_BT_TBL_LOAD_20120524064242.bad|cut -c9-58|wc -c 51 tail -1... (12 Replies)
Discussion started by: donadarsh
12 Replies

3. Shell Programming and Scripting

Problem with cut

I need to read in a file and output it without the comments or newlines. The problem is that is not outputting it properly. STUDENTSDETAILFILE="../data/studentDetails.txt" for getlines in `sed '/#/d' $STUDENTSDETAILFILE` do STUDENTID=`echo $getlines | cut -d: -f1` ... (1 Reply)
Discussion started by: nerdbot
1 Replies

4. Shell Programming and Scripting

How to match (whitespace digits whitespace) sequence?

Hi Following is an example line. echo "192.22.22.22 \"33dffwef\" 200 300 dsdsd" | sed "s:\(\ *\ \):\1:" I want it's output to be 200 However this is not the case. Can you tell me how to do it? I don't want to use AWK for this. Secondly, how can i fetch just 300? Should I use "\2"... (3 Replies)
Discussion started by: shahanali
3 Replies

5. Shell Programming and Scripting

find grep whitespace problem

Hey guys iv written a basic function to ask for input location to find then grep a certain string in the file at the location of the find. For some reason it finds the file with the certain string it however it says directory not found on each string before the whitespace eg enter location to... (5 Replies)
Discussion started by: musicmancanora4
5 Replies

6. UNIX for Advanced & Expert Users

whitespace problem

I have a single string as below: Rat run after Cat i.e. there is a single whitespace after Cat. This causes my file to fail. Is there a way I can remove any whitespace at the end of any string. I tried sed 's/ *//g', but it removes all white space and the above string becomes... (10 Replies)
Discussion started by: RubinPat
10 Replies

7. Shell Programming and Scripting

cut problem

Hi, sample datas are : drwxr-xr-x 2 beewin abidev 96 Jun 13 2006 bwin drwxrwxr-x 2 blsmg01 smg 96 Jun 13 2006 blsmg01 drwxr-xr-x 2 ccmdummy ccm_root 8192 Jun 13 2006 ccmdum drwxr-xr-x 5 dipayan users 8192 Oct 29 09:05 dip I want to cut the last field. I use ls -ll | grep... (7 Replies)
Discussion started by: samir_standing
7 Replies

8. Shell Programming and Scripting

Copy files listed in a text file - whitespace problem.

Hi, Say I have this text file <copy.out> that contains a list of files/directories to be copied out to a different location. $ more copy.out dir1/file1 dir1/file2 dir1/file3 "dir1/white space" dir1/file4 If I do the following: $copy=`more copy.out` $echo $copy dir1/file1... (4 Replies)
Discussion started by: 60doses
4 Replies

9. UNIX for Dummies Questions & Answers

grep and cut problem

Hello folks, I have to (e)grep out a certain pattern e.g. <TAG1> from a huge log file which does not have any space as such. The thing is that once I have 'grep'ed out the <TAG1> from the file I need to extract the content within the tags, i.e, <TAG1>Data_To_Be_Extracted</TAG1> The underlined... (9 Replies)
Discussion started by: Rajat
9 Replies

10. UNIX for Dummies Questions & Answers

Cut command problem !

Hi all! Here is my problem : $ more file yougli:passwd:123456:3265:Yepa Yepo:/home/yougli:/bin/ksh As you can see, in the field "information", there are two spaces between "Yepa" and "yepo". My problem is : $ PARAM='more file | cut -d":" -f5' $ echo $PARAM Yepa Yepo Now i only... (2 Replies)
Discussion started by: tomapam
2 Replies
Login or Register to Ask a Question