concatenate is ignoring space


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting concatenate is ignoring space
# 1  
Old 05-04-2012
concatenate is ignoring space

Hi All,I am facing the below problem I have set a variable:
Code:
a=`cat a.txt| grep "mad" | cut -c 30-50`

the output is coming
Code:
echo $a1
10 Mad300 3215

however the actual ouput is
Code:
1.10 Mad300     3215

There are 4spaces between 300 and 3215 so if i do:

echo "$a" I am getting correct output:
Code:
1.10 Mad300     3215

Now I have to concatenate value of a to some variable $b and assign to $c.
Code:
c="$b$a"

So i am not getting the correct value in $c it is having trimmed spaces output.
Code:
1.10 Mad300 3215

Please advice why the spaces are trimmed and how to get the correct value in $c variable.

Last edited by methyl; 05-04-2012 at 07:47 AM.. Reason: Please use code tags. Laid out post for readability. Corrected some grammar and spacing.
# 2  
Old 05-04-2012
Use double quotes around variables to avoid trimming of spaces. Look at the below example carefully. You'll understand what shell is doing to those extra whitespaces.

Code:
[root@host user]# x="hello     world"
[root@host user]# echo $x
hello world
[root@host user]# echo "$x"
hello     world
[root@host user]# y=" earth     mars"
[root@host user]# echo $y
earth mars
[root@host user]# echo "$y"
 earth     mars
[root@host user]# z="$x$y"
[root@host user]# echo $z
hello world earth mars
[root@host user]# echo "$z"
hello     world earth     mars

And please format your post properly. Here's a link on how to use code tags.
This User Gave Thanks to balajesuri For This Post:
# 3  
Old 05-04-2012
Nothing obvious wrong. When checking the vaue of $c, remember the quotes:
Code:
echo "$c"


For future posts, please mention what Operating System and version you are running and what Shell you are using. Also please use CODE tags to preserve space characters in your posts (which are certainly important to this post).
# 4  
Old 05-04-2012
dont use the cat
Code:
 
 
a=$(grep "mad" a.txt | cut -c 30-50)
echo "$a"

you can use awk also

Code:
 
awk '/mad/ {print substr($0,30,20)}' a.txt

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Concatenate integer and space/newline for cout in C++

I'm trying to print out integers and space/newline for a nicer output, for example, every 20 integers in a row with ternary operator. In C I could do it with:printf("%d%s",tmp_int, ((j+1)%20) ? "\t":"\n"); but could not figure out the equivalent in C++: cout << ((j+1)%20)?... (4 Replies)
Discussion started by: yifangt
4 Replies

2. Cybersecurity

Server is ignoring slashes

When I try to access my website's Administrator page (mysite.com/administrator), I'm redirected to (mysite.comadministrator), as if the slash was removed from the URL The funny thing is that I can access it if I enter 2 slashes (mysite.com//administrator) Any ideas of what might be causing it? (4 Replies)
Discussion started by: rlopes
4 Replies

3. Shell Programming and Scripting

ignoring case

in if clause , how 1 can ignore case (i.e. small or capital) ny suggestions? (2 Replies)
Discussion started by: Gl@)!aTor
2 Replies

4. Shell Programming and Scripting

Ignoring a Pattern

Hi, I have a requirement to find and replace contents from multiple files. I am able to replace the contents. but i am facing a issue while replacing when finding a certain charcter pattern as unix is treating this is a command. Please find below sample on the same Output file to be generated... (1 Reply)
Discussion started by: seeki
1 Replies

5. Shell Programming and Scripting

AWK - Ignoring White Space with FS

I have an AWK script that uses multiple delimiters in the FS variable. FS="+" My awk script takes a file name such as this: 12345_smith_bubba_12345_20120215_4_0.pdf and parses it out based on the under score. Each parsed field then has some code for data validation etc. This script has... (12 Replies)
Discussion started by: reno4me
12 Replies

6. Shell Programming and Scripting

Ignoring certain extensions

Dear Friends, I want to move all the files to temp folder except files having following extensions which are case sensitive. .ttM .Hmt .dMt Request you to guide me to do the same Thank you in advance Anushree (3 Replies)
Discussion started by: anushree.a
3 Replies

7. Shell Programming and Scripting

Calculate total space, total used space and total free space in filesystem names matching keyword

Good afternoon! Im new at scripting and Im trying to write a script to calculate total space, total used space and total free space in filesystem names matching a keyword (in this one we will use keyword virginia). Please dont be mean or harsh, like I said Im new and trying my best. Scripting... (4 Replies)
Discussion started by: bigben1220
4 Replies

8. Shell Programming and Scripting

Ignoring several lines at once in cshell

Hi We use # sign to ignore any line (i.e. comment ). But is it possible to ignore group of line at once or i have to use # in front of each line. Thanks Sarbjit (3 Replies)
Discussion started by: sarbjit
3 Replies

9. UNIX for Dummies Questions & Answers

wc of characters in a file ignoring white space

Hi everyone, $ more abcdefg.ksh abcdef alpha beta gamma abcdef abcdef lmnop $ wc sachin1.ksh 5 7 132 abcdefg.ksh if you see it shows that file has got 240 characters. I actually want to count how many characters... (1 Reply)
Discussion started by: sachin.gangadha
1 Replies

10. Solaris

Dvd files concatenate error: Not enough space

Hi, I downloaded the latest sparc ver of sol10, tried to concatenate the files and encounter error: Not enough space on the disk. Tried from both sol10 x86 and winxp. command used: UNIX: cat file1 file2 file3 file4 file5 > file.iso DOS: copy /b file1 + file2 + file3 + file4 + file5 file.iso... (3 Replies)
Discussion started by: maag
3 Replies
Login or Register to Ask a Question