LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-29-2005, 03:17 AM   #1
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Send string to std output without interpreting (script)


I have a string containing "\ " that I need to send to standard output (in bash) without losing the "\". The only way I know of to send it to standard out in string form (i.e. $string) is with echo, but that will interpret the "\ " as " ", even with the -E flag. The work-around now is to add a few more "\" using 'sed "s/\\\ /\\\\\\\\\ /g"' (string is assigned with grep, so this is placed in between), but then the string has "\\\", so after the point where I echo it to something else I need to remove the extra "\\" before passing the same string as an argument. I guess another solution would be to send the string to sed as an argument without using |. I appreciate any help. Thanks.
ta0kira


PS Here is what I am dealing with:
Code:
#!/bin/bash

#...

grep "." $file | sed "s/\\\ /\\\\\\\\\ /g" | while read lines
do
{
  begin="`echo "$lines" | sed -r "s/$end$//"`"
}
done
I need a replacement for echo in this case so I can take out the ridiculous sed command before "while".
 
Old 06-29-2005, 03:39 AM   #2
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
add -r option to read to get rid of your sed expression

Code:
while read -r line; do
    echo $line
done < $file
I don't know how your file contents look like,
you may be able to get rid of the read function
as well...
 
Old 06-29-2005, 04:33 AM   #3
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Original Poster
Rep: Reputation: Disabled
The problem seems to only be with echo. A line of the file looks something like this:
Code:
New\ Folder /etc/New\ Folder
When I grep or cat this without piping, it shows up on the screen correctly (with the "\"s), but if you were to assign the result of the grep to a string and echo the string then it would show up like this:
Code:
New Folder /etc/New Folder
The way around this was to turn the line from the file into
Code:
New\\\ Folder /etc/New\\\ Folder
so that echo would send
Code:
New\ Folder /etc/New\ Folder
to the sed command, but then if I were to use the string as an argument for ln (etc.) later then it would see argument 1 as New\ Folder instead of taking the \ to mean that the space is a part of the name; "\\\ " becomes "\ " on the command line, whereas "\ " becomes " ":
Code:
ln -s New\ Folder /etc/New\ Folder

#vs

ln -s New\\\ Folder /etc/New\\\ Folder

[edit]
#I could use something like:
ln -s `echo "$lines"`
#but I am trying to avoid being sloppy by not adding the extra \\ to begin with
Thanks for your help.
ta0kira

Last edited by ta0kira; 06-29-2005 at 04:40 AM.
 
Old 06-29-2005, 06:29 AM   #4
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
I don't see your grep patern, so I try with 'Folder' :

Code:
grep Folder $file | while read -r line; do
    echo "ln -s $line"
done
 
Old 06-29-2005, 07:32 AM   #5
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Original Poster
Rep: Reputation: Disabled
Ok, so are you saying it's 'read' that takes away the "\" and -r will keep it from doing that? I know echo also takes it away, so I'll try read with -r and echo with -E. So there is no other way to display a string other than echo? Thanks again.
ta0kira
 
Old 06-29-2005, 08:24 AM   #6
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
Quote:
I know echo also takes it away
Not on my shell, GNU bash, version 3.00.15(2)-release (i486-slackware-linux-gnu)
Code:
$ echo "\hello \I \have\ \backslashes"
\hello \I \have\ \backslashes
$
Quote:
there is no other way to display a string other than echo?
There are other way, sure
Code:
printf "%s\n" "\hello \I \have\ \backslashes"

Last edited by keefaz; 06-29-2005 at 08:27 AM.
 
Old 06-29-2005, 08:50 AM   #7
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Original Poster
Rep: Reputation: Disabled
Quote:
Not on my shell, GNU bash, version 3.00.15(2)-release (i486-slackware-linux-gnu)
Interesting. I'll have to double check mine, and also that read flag.
Quote:
There are other way, sure
Thanks. I'd feel silly using a C function in bash
ta0kira
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Need help interpreting tcpdump output line wrw3 Linux - Networking 0 10-29-2005 07:47 PM
Interpreting the output of 'lspci' command. drminix Linux - Hardware 2 06-14-2005 03:20 AM
C++ std::string to int Slaxx Programming 1 10-30-2004 10:03 PM
Send query string via MIRC script cadj Programming 0 08-25-2004 06:25 AM
bash-script: output text between two ocurrences of a specific string isl01jbe Programming 1 06-17-2004 02:36 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:16 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration