Cut and WhiteSpace Problem


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Cut and WhiteSpace Problem
# 1  
Old 09-13-2010
Cut and WhiteSpace Problem

Hi,

I have a shell script that reads a parameter file to set variables. I have an issue when the parameter I try to read contains whitespace.

e.g

File Contents

Quote:
N|N|N|Fileencode.txt|This is the first one
Code

Quote:
for PROCESS_RECORD in `grep -i "^${PRCSNAME}" ${PARAMETER_OUTBOUND}` ; do

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

done
The result is SUBJECT is set to

Quote:
This
and I want subject set to

Quote:
This is the first one
I've tried different variations but nothing seems to work.

Can anyone help please
# 2  
Old 09-13-2010
This is weird cause it works fine on my test machine:
Code:
# echo "N|N|N|Fileencode.txt|This is the first one" | cut -d "|" -f5
This is the first one

Maybe try:
Code:
cut -d "|" -f5-

# 3  
Old 09-13-2010
Code:
SUBJECT=`echo ${PROCESS_RECORD} | cut -d "|" -f5-`

# 4  
Old 09-13-2010
Quote:
Originally Posted by bartus11
This is weird cause it works fine on my test machine:
Code:
# echo "N|N|N|Fileencode.txt|This is the first one" | cut -d "|" -f5
This is the first one

Maybe try:
Code:
cut -d "|" -f5-

yes, that works fine on mine, but the input file has multiple lines.

I tried the second option and it gives me the same incorrect result.

when it tries to process the second line of input it treats it as if it was

Quote:
is|is|is|is|is
# 5  
Old 09-13-2010
Post an example of the input file and the desired output.
# 6  
Old 09-13-2010
Quote:
Originally Posted by Franklin52
Post an example of the input file and the desired output.
File Contents

Quote:
N|N|N|N|This is the first one
N|N|N|N|This is the second one
Quote:
for PROCESS_RECORD in `grep -i "^${PRCSNAME}" ${PARAMETER_OUTBOUND}` ; do

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

done
For the first iteration SUBJECT should =

Quote:
This is the first one
For the Second iteration SUBJECT should =

Quote:
This is the second one
# 7  
Old 09-13-2010
Check if any of these work. I tested on Bash on Cygwin.

Code:
$
$ cat f16
N|N|N|N|This is the first one
N|N|N|N|This is the second one
$
$
$ ## 1
$ while read LINE; do
    x=`echo $LINE | cut -d "|" -f5-`
    echo $x
  done < f16
This is the first one
This is the second one
$
$ ## 2
$ while read LINE; do
    x=`echo $LINE | cut -d "|" -f5`
    echo $x
  done < f16
This is the first one
This is the second one
$
$ ## 3
$ OLD_IFS="$IFS"
$ IFS="|"
$ while read C1 C2 C3 C4 SUBJECT; do
    echo $SUBJECT
  done < f16
This is the first one
This is the second one
$ IFS="$OLD_IFS"
$
$ ## 4
$ perl -F'\|' -lane 'print $F[4]' f16
This is the first one
This is the second one
$
$ ## 5
$ awk -F"|" '{print $5}' f16
This is the first one
This is the second one
$
$ ## 6
$ perl -lne 'print substr($_,rindex($_,"|")+1)' f16
This is the first one
This is the second one
$
$ ## 7
$ perl -lne '/^.*\|(.*)$/ && print $1' f16
This is the first one
This is the second one
$
$
$

HTH,
tyler_durden
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