Trim a new line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Trim a new line
# 1  
Old 10-03-2010
Trim a new line

Okay, I am trying to make a bash script to get a certain domains IP address (my home ip). My home is on a DHCP lease from my ISP, so I cannot always trust the IP address to remain constant.

This is what I have so far for it:

Code:
alias ip-home="ping -c 1 example.com | grep 'PING' | cut -d'(' -f2 | cut -d')' -f1";

But that outputs a line break afterwords. What I would really like is something that matches the output of:

Code:
curl -q www.whatismyip.com/automation/n09230945.asp

which is...

Code:
dev:~ tnanek$ curl -q www.whatismyip.com/automation/n09230945.asp
0.0.0.0dev:~ tnanek$

The IP was altered above, as well as the domain in the first statement.

I am on Snow Leopard 10.6.4, though it is built off of a Unix core.

For background on what I am trying to do ultumatly...

I am a Drupal developer, and I would like my .bashrc file to set my host entry appropriately to use a specific domain (not one set via my router) to point to my home computer. I will ultimatly be adding that, but meanwhile, now I'm only getting the ip address in a usable form.
# 2  
Old 10-03-2010
Try:
Code:
host example.com | awk 'NR==1{printf $NF}'

This User Gave Thanks to Scrutinizer For This Post:
# 3  
Old 10-03-2010
Scrutinizer's is better, but tr -d '\n' is often handy to know.
Code:
# ping -c 1 google.com | grep 'PING' | cut -d'(' -f2 | cut -d')' -f1 | tr -d '\n'
74.125.65.104#

This User Gave Thanks to fubaya For This Post:
# 4  
Old 10-03-2010
When entering that in the command line it works just fine; however when putting that in a bash script it does not. Here is an example of how I'm getting it (I know echo adds a line break after, but thats not the issue).

What I want is just the IP address, no line breaks before or after, in a variable that I can then insert into my local host file, after some tweaking of adding my desired access domain to it.

Here's my command line:
Code:
dev:~ tnanek$ ip-home
hostdev:~ tnanek$

And here's the relevant portion of the .bashrc file:

Code:
homeip="host example.com | awk 'NR==1{printf $NF}'";
alias ip-home="echo $homeip";



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

And same with your method fubaya; works fine directly on the command line, but doesn't work in .bashrc. Here are my modified examples:

My command line:
Code:
dev:~ tnanek$ ip-home
dev:~ tnanek$

My .bashrc:
Code:
homeip="ping -c 1 example.com | grep 'PING' | cut -d'(' -f2 | cut -d')' -f1 | tr -d '\n'";
alias ip-home="echo $homeip";



---------- Post updated at 12:07 PM ---------- Previous update was at 11:45 AM ----------

Got it using backquotes as per
Code:
http://ubuntuforums.org/showthread.php?t=330936

Working version of fubaya's method:
Code:
homeip=`ping -c 1 example.com | grep 'PING' | cut -d'(' -f2 | cut -d')' -f1 | tr -d '\n'`;
alias ip-home="echo $homeip";

And of Scrutinizer's method:
Code:
homeip=`host example.com | awk 'NR==1{printf $NF}'`;
alias ip-home="echo $homeip";

# 5  
Old 10-03-2010
Alternatively you could use a function instead of an alias:
Code:
function ip-home 
{
  host example.com | awk 'NR==1{printf $NF}'
}

# 6  
Old 10-03-2010
Quote:
Originally Posted by tnanek
What I want is just the IP address, no line breaks before or after, in a variable that I can then insert into my local host file, after some tweaking of adding my desired access domain to it.

Command substitution trims trailing newlines, so I don't even understand how you're having a problem with a trailing newline(s) in a variable whose value is assigned from command substitution.

In the following shell session, while the first printf statement generates 5 trailing newlines (verified with od), not a single one is assigned to the variable $v (again, verified with od). If your shell does not work this way, then it's an egregious violation of the posix standard. It's more likely that you are doing something wrong when you are accessing/printing the variable's value:
Code:
$ printf 'a\n\n\n\n\n'
a




$ printf 'a\n\n\n\n\n' | od -c
0000000    a  \n  \n  \n  \n  \n                                        
0000006
$ v=$(printf 'a\n\n\n\n\n')
$ echo $v
a
$ printf %s "$v" | od -c
0000000    a                                                            
0000001
$ printf %s "$v"        
a$<my shell prompt here without preceding newline>

Regards,
Alister

Last edited by alister; 10-03-2010 at 07:40 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Trim Filename

Hi All, I have a file named as FAB1_600015_CONRAD.A0_7XYZ12345.000_LT-SWET.01_LTPA25L_20110622-161429_07_WFR12345_20110622-161429_20110712-125228.data.dis I want to generate a directory taking only the 7XYZ12345.000_WFR12345 The length and format of the Filename will be the same... (2 Replies)
Discussion started by: asheshrocky
2 Replies

2. Shell Programming and Scripting

get the fifth line of a text file into a shell script and trim the line to extract a WORD

FOLKS , i have a text file that is generated automatically of an another korn shell script, i want to bring in the fifth line of the text file in to my korn shell script and look for a particular word in the line . Can you all share some thoughts on this one. thanks... Venu (3 Replies)
Discussion started by: venu
3 Replies

3. Shell Programming and Scripting

To trim Certain field in a line of a file and replace the new string in that position

To trim 3rd field in for all the lines of a file and replace the modified string in that particular field. For example i have a file called Temp.txt having content Temp.txt ----------------- 100,234,M1234 400,234,K1734 300,345,T3456 ---------------- So the modified file output should... (4 Replies)
Discussion started by: rpadhi
4 Replies

4. Shell Programming and Scripting

another trim question using tr

Hi all, I've been looking for how to eliminate blank spaces in a variable or strings. I've seen several ways, using sed, awk and even python. One of them is use 'tr' command, but it does not work as I expected: For example: echo " stuff " | tr -s " "leaves one space ahead and another... (3 Replies)
Discussion started by: AlbertGM
3 Replies

5. Shell Programming and Scripting

Trim issue

Hi All, I am using trim in my code.. ID_SA_SOURCE="`echo "$data" | cut -c17-34 | tr -s " "`" ID_SA_DEST="`echo "$data" | cut -c35-52 | tr -s " "`" echo"$ID_SA_SOURCE";"$ID_SA_DEST"; the output is 0608166896; 3001339; contains one whitespace between the two ..how can i remove that single... (3 Replies)
Discussion started by: scorpio
3 Replies

6. Shell Programming and Scripting

Trim

Hello, I am passing a filename to a script to draw parameters from it. However, I want to use part of the filename as a parameter. The filename is transfer_ccf_3731_10.sh but I only need the 3731_10 part of it. Is this possible? Any help or suggestions would be appreciated! Regards, J. (4 Replies)
Discussion started by: JWilliams
4 Replies

7. Shell Programming and Scripting

To Trim spaces at the end of line

Hi Friends, Can any one help with this issue: How to trim spaces for each line at the end, Like I have a file in this format. EMP1 SMITH 46373 5 STREET HOWARD 74636 EMP2 JONES 5454 { these are spaces ........} EMP3 SMITH 46373 5 STREET HOWARD 74636 EMP4 JON 2554 { these are... (1 Reply)
Discussion started by: sbasetty
1 Replies

8. Shell Programming and Scripting

Trim whitespace and add line break

All, I'm a newbie at shell scripting and regular expressions and I just need to take a file that's arranged like the one below, remove all leading and trailing whitespace and add a line break after each word. I've been able to remove a few spaces using various awk, sed and Perl scripts, but... (7 Replies)
Discussion started by: moose1
7 Replies

9. Shell Programming and Scripting

Trim trailing spaces from each line in a file

Hello folks, Is there a simple way to trim trailing spaces from each line a file. Please let me know. Regards, Tipsy. (5 Replies)
Discussion started by: tipsy
5 Replies

10. Shell Programming and Scripting

trim whitespace?

I'm trying to find a command that will trim the white space off a string. e.g. $str = " stuf " $str = trim ( $str ) echo $str // ouput would just be stuf Thanks, Mark (4 Replies)
Discussion started by: msteudel
4 Replies
Login or Register to Ask a Question