Cut and WhiteSpace Problem


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Cut and WhiteSpace Problem
# 15  
Old 09-14-2010
Quote:
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

These remarks make no sense unless perhaps you are referring to your original script with the "for" statement.
# 16  
Old 09-14-2010
Quote:
Originally Posted by methyl
These remarks make no sense unless perhaps you are referring to your original script with the "for" statement.
Nope, the Read version. It doesn't make any sense to me either, but it now works with those pipes on the end.
# 17  
Old 09-14-2010
Might be because the example data has an inconsistent number of fields.

Code:
Post #1
N|N|N|Fileencode.txt|This is the first one 

Post #11
H1|Y|This is the First Line
H1|N|This is the Second Line

# 18  
Old 09-14-2010
Quote:
Originally Posted by methyl
Might be because the example data has an inconsistent number of fields.

Code:
Post #1
N|N|N|Fileencode.txt|This is the first one 

Post #11
H1|Y|This is the First Line
H1|N|This is the Second Line

No, they were just examples, the files I were running against with real data were fine.
# 19  
Old 09-14-2010
As methyl pointed out, your sample data's format has been inconsistent throughout this thread. If you are interested in the following code but it does not work, provide a sample of the data against which it is actually being run.

The data I'm using:
Code:
$ cat File1.txt 
H1|Y|This is the First Line
H1|N|This is the Second Line

The script:
Code:
$ cat greygor.sh 
#!/bin/sh

prcsname='^H1'
parameter_outbound=File1.txt

grep -i "$prcsname" "$parameter_outbound" |
  while IFS=\| read -r f1 f2 f3; do
     var1=$f2
     subject=$f3
     echo var1=\""$var1"\" subject=\""$subject"\" 
  done

The result:
Code:
$ ./greygor.sh
var1="Y" subject="This is the First Line"
var1="N" subject="This is the Second Line"

Regards,
Alister

---------- Post updated at 11:07 AM ---------- Previous update was at 10:53 AM ----------

If the first field needs to match exactly and regular expressions are not required:
Code:
#!/bin/sh

prcsname='H1'
parameter_outbound=File1.txt

while IFS=\| read -r f1 f2 f3; do
    if [ "$prcsname" == "$f1" ]; then
        var1=$f2; subject=$f3
        echo var1=\""$var1"\" subject=\""$subject"\"
    fi
done < "$parameter_outbound"

# 20  
Old 09-14-2010
Quote:
Originally Posted by alister
As methyl pointed out, your sample data's format has been inconsistent throughout this thread. If you are interested in the following code but it does not work, provide a sample of the data against which it is actually being run.
Hi, thanks for the help.

My data has been consistent with each code sample it's been presented with Smilie

As I said before it is working now but thanks for all the great suggestions, you guys here are really helpful.

I would have used the actual data but it's propriety hence the dummy data that recreates the real issue I was having.

I will give the code you gave a try against the original format config file, but as it's working against the new config file with a "|" added to the end there is no real problem.

And again, thanks to all that replied it was really useful and I learned a few new things along the way.
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