Include white spaces while using CUT command


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Include white spaces while using CUT command
# 43  
Old 05-27-2010
Quote:
Originally Posted by Sekar1
$ rm final.txt

---------- Post updated at 07:39 PM ---------- Previous update was at 07:37 PM ----------

Hi I got the output :-)
Code:
$ rm final.txt
$ . ./css2.sh
$ cat final.txt
IIT                    Chennai

Requesting you to please explain me what changes you did.
I put double quotes around $line and $var.

If the argument to the echo command does not have double quotes around it, then the shell substitutes multiple spaces by a single space.

See below:

Code:
$
$ # parameters without enclosing double quotes passed to the echo command
$ echo Gone               with       the                           wind
Gone with the wind
$
$ # As you can see, there's only one space between "Gone" and "with"
$ # and between "with" and "the"; and "the" and "wind"
$ # The shell did that. That is the shell's default behavior.
$ #
$ # Now pass the same parameters with enclosing double quotes to the echo command
$ # (To be technically accurate, this now is just one argument, as opposed to four arguments in the earlier case)
$
$ echo "Gone               with       the                           wind"
Gone               with       the                           wind
$
$ # the embedded spaces are preserved now
$

In the following line of your old script:

Code:
echo $line | sed 's/.*<college>\(.*\) <\/college>$/\1/'

the value of the "line" shell variable was:

Code:
<Record><sno>1</sno><empid>E0001</empid><name>Rejsh suderam</name><college>IIT Chennai </college>

The shell had substituted multiple spaces between "IIT" and "Chennai" by a single space.

But in your new script, spaces were preserved between "IIT" and "Chennai" in the following line -

Code:
echo "$line" | sed 's/.*<college>\(.*\) <\/college>$/\1/'

And same is the case while printing out to the target file.

Code:
echo "$var" >> $targetfile

You have to put the double-quotes in both the places. Otherwise it's quite possible that your script preserves multiple spaces while assigning the value to var but loses it due to the echo while redirecting its value $var to the target file.

HTH,
tyler_durden

Last edited by durden_tyler; 05-27-2010 at 05:21 PM..
# 44  
Old 05-28-2010
Many thanks for explaining. Please let me know how the below SED command works. What is the use in specifying 1?

sed 's/.*<college>\(.*\) <\/college>$/\1/'

---------- Post updated at 10:35 AM ---------- Previous update was at 10:32 AM ----------

Also please explain me, whether the SED command alone works and prints the above desired output in the file? Is it not necessary to read the file line by line?
Please provide me optimal solution. Thanks in advance.

---------- Post updated at 10:45 AM ---------- Previous update was at 10:35 AM ----------

Seems to be the above script is working if the college tag is present in the first line of source file.
If my source file as below format

Code:
$ cat source.txt
<Record><sno>1</sno><empid>E0001</empid><name>Rejsh   suderam</name><college>IIT                    Chennai   </college>
<Record><sno>1</sno><empid>E0001</empid><name>Rejsh   suderam</name><placeofbirth>chennai     mylapore   </placeofbirth><dateofbirth>22ndmay99</dateofbirth><college>IIT                    Chennai   </college><course>MCA</course>

and if i execute the above command,i am receiving the below output. Please correct if any small changes i need to update in the code.

Code:
$ . ./css2.sh
$ cat final.txt
IIT                    Chennai
<Record><sno>1</sno><empid>E0001</empid><name>Rejsh   suderam</name><placeofbirth>chennai     mylapore   </placeofbirth><dateofbirth>22ndmay99</dateofbirth><college>IIT                    Chennai   </college><course>MCA</course>

# 45  
Old 05-28-2010
sed is matching the whole line and segragating the only string within the <college> and </college>. As you can see, that is enclosed with the operators \( and \). The string inside this operator is your target string which is "IIT Chennai". And \1 corresponds to this string. So in a nutshell, it is replacing the whole string in the line with only "IIT chennail" which you wanted. Hope this helps.
# 46  
Old 05-28-2010
oh... thanks for explaning. Requesting you to please solve my above query
# 47  
Old 05-28-2010
ok use this instead. It is always good to give a sample I/P when you are requesting a solution as it will help us to see the data.

Code:
sed 's/.*<college>\(.*\) <\/college>.*/\1/'

# 48  
Old 05-28-2010
Hi Many thanks for your solution. I got the result. Could you please explain me what is the difference in using $ and .*? Also shall i able use this command alone and able to get the desired output?. Once thanks many thanks to Devtakh and Tyler_durdenfor answering all my queries.
Code:
sed 's/.*<college>\(.*\) <\/college>$/\1/'
sed 's/.*<college>\(.*\) <\/college>.*/\1/'

# 49  
Old 05-28-2010
Most welcome

$ - will match if the line has <\college> as the last string in the line and nothing else
and .* - will match even if there are other strings in the line after </college>
FYI,
^ -> instructs to match a string in the begining of the line
$ -> instructs to match a string in the end of the line
. -> means any character
* -> zero or more occurance of the preceeding character

so
.* -> means match zero or more occurance of any character.

hope this clears your doubts.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Include information and change column order while maintaing white spaces

I need to change the order of the columns of data that looks like this: The original data is in 6 tab-separated columns. FACTSHEET factsheet NN 1 5 DEP WHAT what WP 2 3 SBJ IS be VBZ 3 1 NMOD AIDS AIDS NP ... (1 Reply)
Discussion started by: owwow14
1 Replies

2. Shell Programming and Scripting

Shell command to Strip white spaces at specific location

Hello, I have a Comma separated input file with one of the sample records as below: -9223372036854477528,"834","834003325515BXNI00101012013C","5","PLAN_VALUE","PPO, General Cable","C",20130101,99991231,"A","2012-12-25-13.58.14.434000","ZPL2 ","2012-12-25-13.58.14.434000","ZPL2 ... (4 Replies)
Discussion started by: Praveenkulkarni
4 Replies

3. Shell Programming and Scripting

Leading white spaces

Hi, I am having problem in deleting the leading spaces:- cat x.csv baseball,NULL,8798765,Most played baseball,NULL,8928192,Most played baseball,NULL,5678945,Most played cricket,NOTNULL,125782,Usually played cricket,NOTNULL,678921,Usually played $ nawk 'BEGIN{FS=","}!a... (2 Replies)
Discussion started by: scripter12
2 Replies

4. Shell Programming and Scripting

Cut command skips spaces

Hi everyone I have a file looks like this X01 1 JOE 20100312 X02 TOD 20100312 X03 3 SAM 20100312 I wrote a script to assign the values in 5 columns to 5 variables. When I use cut command to assign column 2 or 3, it skips it if it is a space (see below) ... (3 Replies)
Discussion started by: nimo
3 Replies

5. Shell Programming and Scripting

How to Use Sed Command to replace white spaces with comma from between two fields - Mayank

SHELL SCRIPT Hi I have a file in the following format Mayank Sushant Dheeraj Kunal ARUN Samir How can i replace the white space in between and replace them with a comma?? The resultant output should be Mayank,Sushant Dheeraj,Kunal ARUN,Samir i tried using sed -e... (8 Replies)
Discussion started by: mayanksargoch
8 Replies

6. UNIX for Dummies Questions & Answers

Removing Double Spaces for cut command

Hi I have a shell script that looks for running processes in order to kill them. The script (ksh) gets the PID of these processes using the following: PROCS=`ps -fu ${USERID} | egrep "MONITOR" | grep -v grep | cut -d" " -f4` However, I spotted an issue where PID's of different lengths... (3 Replies)
Discussion started by: mikem22
3 Replies

7. Shell Programming and Scripting

Two or more white spaces in string

Hi, Can anybody suggest me how to combine two strings with two or more white spaces and assign it to a variable? E.g. first=HAI second=HELLO third="$first $second" # appending strings with more than one white spaces echo $third this would print HAI HELLO Output appears... (2 Replies)
Discussion started by: harish_oty
2 Replies

8. Shell Programming and Scripting

trimming white spaces

I have a variable that calls in a string from txt file. Problem is the string comes with an abundance of white spaces trailing it. Is there any easy way to trim the tailing white spaces off at the end? Thanks in advance. (9 Replies)
Discussion started by: briskbaby
9 Replies

9. Shell Programming and Scripting

delete white spaces

hi all... i have the next question: i have a flat file with a lot of records (lines). Each record has 10 fields, which are separated by pipe (|). My problem is what sometimes, in the first record, there are white spaces (no values, nothing) in the beginning of the record, like this: ws ws... (2 Replies)
Discussion started by: DebianJ
2 Replies

10. UNIX for Dummies Questions & Answers

deleting white spaces

How would I delete white spaces in a specified file? Also, I'd like to know what command I would use to take something off a regular expression, and put it onto another. ie. . . . expression1 <take_off> . . . expression2 (put here) . . . Any help would be great, thanks! (10 Replies)
Discussion started by: cary530
10 Replies
Login or Register to Ask a Question