chomp like Perl operator in Bash


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting chomp like Perl operator in Bash
# 1  
Old 05-07-2010
chomp like Perl operator in Bash

I am sure there should exist a chomp like Perl operator in Bash using which I can literally remove new line characters as show below:

Quote:
use strict;
use warnings;

my $str1 = "Hello\n";
my $str2 = "World";
print $str1.$str2;

print "\n";

chomp($str1);

print $str1.$str2;

print "\n";
Any clue?

Last edited by paragkalra; 05-07-2010 at 01:48 AM..
# 2  
Old 05-07-2010
Code:
#!/bin/bash
str1="Hello\n"
str2="World"
echo -e "$str1.$str2"
echo -e "\n"

str1="${str1%\\n}"

echo -e "$str1.$str2"
echo -e "\n"

# 3  
Old 05-07-2010
It worked Smilie

I was anticipating some function. Not sure what this line is doing:

Quote:
str1="${str1%\\n}"
# 4  
Old 05-07-2010
${STRING%SUBSTRING} removes SUBSTRING from the end of STRING
\n : the \ must be escaped by \ so thats why i wrote \\n
This User Gave Thanks to frans For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash variable assignment failure/unary operator expected

I have a little code block (executing on AIX 7.1) that I cannot understand why the NOTFREE=0 does not appear to be assigned even though it goes through that block. This causes a unary operator issue. #!/bin/bash PLATFORM="AIX" NEEDSPC=3000 set -x if ; then lsvg | grep -v rootvg | while... (6 Replies)
Discussion started by: port43
6 Replies

2. Shell Programming and Scripting

Operator test in bash

Hello, can you please help me because I am totally confused with a simple script: #!/bin/bash ] || ] && echo "Good Morning" ] || ] && echo "Good Night" For me, these two strings are indentical: false || false and there is no point to execute echo command. But the run result is... (5 Replies)
Discussion started by: AndreiM
5 Replies

3. Shell Programming and Scripting

Equivalent to Perl's and Bash's "=~" Operator?

Hello All, Alright, so this is driving me absolutely insane. I can't seem to find this ANYWHERE... I've tried every combination and synonym I can think of for this trying to search Google. What is the Expect/Tcl equivalent to Perl and Bash's "=~" operator, (i.e. the "contains" operator).... (2 Replies)
Discussion started by: mrm5102
2 Replies

4. Shell Programming and Scripting

bash script error with binary operator expected.

Hello, I am not sure, where I am missing in the scirpt, I am trying to grep few users from /etc/passwd file and if exists, I added line to echo as user exist, if not create it. #!/bin/bash for vid in v707 z307 z496 z163 z292 ; do if then echo " $vid User exists " else ... (2 Replies)
Discussion started by: bobby320
2 Replies

5. Shell Programming and Scripting

Perl Diamond Operator

I know that when using 'while (<FILE>) {}', Perl reads only one line of the file at one time, and store it in '$_'. Can I change some parameters so that 'while (<>) {}' can read more than one lines, like 2 or 5 lines at one time? Thanks for the help! (1 Reply)
Discussion started by: zx1106
1 Replies

6. Shell Programming and Scripting

FIle (directory) test operator (bash)

I'm almost pulling out my hair trying to figure out what's wrong with this... there's no reason I can see that it shouldn't be working. It seems that the code acts as though the conditional statement is true no matter what - I've even tried removing the negation operator, but it always goes into... (5 Replies)
Discussion started by: wildbluefaerie
5 Replies

7. Shell Programming and Scripting

Diamond operator in Until Statement (perl)

Hello: I have the following perl script which is giving me trouble inside the second elsif statement. The purpose of the script is to go through a file and print out only those lines which contain pertinent information. The tricky part came when I realized that certain items actually spanned... (5 Replies)
Discussion started by: erichpowell
5 Replies

8. Shell Programming and Scripting

scalar variable assignment in perl + { operator

When reading over some perl code in a software document, I came across an assignment statement like this $PATH = ${PROJECT}/......./.... In this particular form of scalar variable assignment, what does the curly braces operators do ? Also, what is the benefit in doing scalar assignment this... (3 Replies)
Discussion started by: JamesGoh
3 Replies

9. UNIX for Dummies Questions & Answers

Help in modulus operator in Bash

Hi, I would like to know given that i have two columns and I would like to take the positive integer of the differences between the two columns. which means |3-2|=1; |2-3|=1 as well. I would like to know do Bash recognize | | as well for this purposes? Thanks. -Jason (2 Replies)
Discussion started by: ahjiefreak
2 Replies

10. Shell Programming and Scripting

chomp and chop in perl

What is the differnece between chomp and chop in perl? (1 Reply)
Discussion started by: seismic_willy
1 Replies
Login or Register to Ask a Question