Linux, FreeBSD, Juniper, Cisco / Network security articles and troubleshooting guides

FAQ
It is currently Tue Jan 28, 2020 12:18 pm


News News of Shell Scripting and Programming

Site map of Shell Scripting and Programming » Forum : Shell Scripting and Programming

Tips & Tricks, Questions regarding shell scripts, awk, perl, sed and much more.

 [ Total topics 31 Go to page 1, 2, 3, 4

Message
 Post subject: How to replace \w SED multiple matches on the same line without piping
PostPosted: Tue Oct 08, 2013 6:29 am 
SED's replacing mechanism replaces only the first occurrence each line. To change this, use the "/g" flag.

Example when sed replaces first match only:
Code:
[root@rhel5 ~]# echo "test test asdf test" >test.html
[root@rhel5 ~]# echo "test test asdf test" >>test.html
[root@rhel5 ~]# sed -i 's/test/replacement/' test.html
[root@rhel5 ~]# cat test.html
replacement test asdf test
replacement test asdf test


Example when sed replaces all matches on each line:
[root@rhel5 ~]# echo "test test asdf test" >test.html ...

Read more : How to replace \w SED multiple matches on the same line without piping | Views : 2614 | Replies : 0

Top
 Post subject: Wget: How to test web server gzip compression
PostPosted: Wed Jun 26, 2013 8:39 am 

What is wget


Wget and curl are two powerful Linux/Unix tools for transfering files over HTTP and FTP. For HTTP, wget is especially useful when it comes to injecting specific headers into an http request, troubleshooting servers' http response headers or testing if a web server has gzip compression enabled. In order for gzip compression to work it needs two components: 1. the client has to inform the server that it accepts gzip encoding (compression) ...

Read more : Wget: How to test web server gzip compression | Views : 3142 | Replies : 0

Top
 Post subject: How to replace new line in perl script
PostPosted: Wed Jun 26, 2013 8:03 am 
In previous article, I showed how to use Linux shell to echo new line. I'm going to use that example for this post.


Echoing multiple new lines:
Code:
# echo -e "text\n\n\n"
text



#


Let's say we send this output to a perl script via pipe:
Code:
# echo -e "text\n\n\n" | perl /tmp/perl-new-line.pl   
text#
# cat /tmp/perl-new-line.pl
#!/usr/bin/perl
while(<>){
  $_ =~ s/\n//;
  print "$_";
}


The script takes the input and parses every line ...

Read more : How to replace new line in perl script | Views : 3935 | Replies : 1

Top
 Post subject: How to echo newline in Linux/Unix shell
PostPosted: Tue Jun 25, 2013 5:58 am 
Many programming or scripting languages interpret characters preceded by backslash as special characters. Example "\n" as echo new line.

By default, "echo" Linux shell command does not interpret "\n". To do so, enable the special switch. Quote from "man echo":
Quote:
DESCRIPTION
Echo the STRING(s) to standard output.

-n do not output the trailing newline

-e enable interpretation of backslash escapes

-E disable interpretation of backslash escapes (default)

and look at the difference:
user@host:~> echo ...

Read more : How to echo newline in Linux/Unix shell | Views : 74949 | Replies : 0

Top
 Post subject: Remove empty lines in vi/vim
PostPosted: Mon Apr 26, 2010 11:06 am 
Remove empty lines in files while editing them with vi or vim.
Code:
:%s/^[\ \t]*\n//g
or
Code:
:%s/^\n//g


Both worked for me. Make sure to test it for your environment.

Read more : Remove empty lines in vi/vim | Views : 1531 | Replies : 0

Top
 Post subject: Using sed to comment/uncomment lines in files
PostPosted: Fri Apr 02, 2010 6:07 am 
Using sed to comment or uncomment lines in files:

The following test file has a commented line inside it.
~ cat man.sed
The sed utility reads the specified files, or the standard input if no
files are specified, modifying the input as specified by a list of com-
mands. The input is then written to the standard output.

A single command may be specified as the first argument to sed. Multiple
#commands may be specified ...

Read more : Using sed to comment/uncomment lines in files | Views : 8282 | Replies : 0

Top
 Post subject: Unix shell - using TR to replace new lines with spaces
PostPosted: Thu Feb 25, 2010 8:08 am 
Unix shell - using TR to replace new lines with spaces

There are a few ways of replacing new lines with other characters. Below I'll show a few examples for replacing new lines in strings or standard input/output.

Replace new lines with spaces:

# cat test.file
In the first synopsis form, the characters in string1 are translated into
the characters in string2 where the first character in string1 is trans-
lated into the first ...

Read more : Unix shell - using TR to replace new lines with spaces | Views : 7845 | Replies : 0

Top
 Post subject: SED how to remove multiple white spaces from a string
PostPosted: Thu Feb 25, 2010 7:14 am 
This article will show only a few example that would be enough for most people:

How to remove multiple white spaces from a string:
Code:
# echo 'some     white            spaces' | sed 's/  *//g'
somewhitespaces


How to replace multiple white spaces from a string:
Code:
# echo 'some     white            spaces' | sed 's/  */\ /g'
some white spaces


Above sed command replaces multiple spaces with a single space. You can adjust it to replace multiple ...

Read more : SED how to remove multiple white spaces from a string | Views : 10466 | Replies : 0

Top
 Post subject: Remove the character before the last character with SED - shell scripting
PostPosted: Wed Nov 04, 2009 10:49 am 
Removing the character before the last character with SED - shell scripting.

Today I received a comment to my "How to remove first/last character from a string using SED" article: http://www.ivorde.ro/How_to_remove_first_last_character_from_a_string_using_SED-75.html

Similar to removing the last character (cat files |sed 's/\(.*\)./\1/'), here's how to remove the character before the last character.

Below is a simple rpm -qa output
Code:
# rpm -qa | head
libgcc-3.2.3-54
setup-2.5.27-1
basesystem-8.0-2
glibc-2.3.2-95.39
bzip2-libs-1.0.2-11.EL3.4
cracklib-2.7-22
e2fsprogs-1.32-15.1
ethtool-1.8-3.3
gdbm-1.8.0-20
glib2-2.2.3-2.0


So ...

Read more : Remove the character before the last character with SED - shell scripting | Views : 3169 | Replies : 0

Top
 Post subject: How to convert a text into a list of words, one per line
PostPosted: Wed Aug 05, 2009 7:02 am 
Depending on the type of processing you need to do on any file, it's possible you meight need to convert it into a list of words, with one word per line.

# cat test.file
FreeBSD 7.2-RELEASE is now available for the amd64, i386, ia64, pc98, powerpc, and sparc64 architectures.

FreeBSD 7.2 can be installed from bootable ISO images or over the network; the required files can be downloaded via FTP or BitTorrent as described in ...

Read more : How to convert a text into a list of words, one per line | Views : 8159 | Replies : 0

Top
 [ Total topics 31 Go to page 1, 2, 3, 4


Last 10 active topics


Virtualization

No new posts How to retrieve Vmware ESXi license from ssh shell command line
View the latest post

Shell Scripting and Programming

No new posts Scan IP range using nmap
View the latest post
No new posts Password generator with user inputs bash script
View the latest post

TCP/IP Networking

No new posts TCP ack after two packets
View the latest post

Junos Tips & Tricks

No new posts MTR / My traceroute in Junos
View the latest post

VPS Hosting & Security

No new posts Host a gmod server on a vps or shared host?
View the latest post

Juniper virtual MX (vMX)

No new posts Juniper Virtual MX (vMX) 14.2R5.3 Phase 2 (RE and MPC on different VMs)
View the latest post

OpenLDAP - Lightweight Directory Access Protocol

No new posts cannot login in linux system with ldap user after changing the ldap user password
View the latest post

PHP and other web technologies

No new posts php
View the latest post

TCPdump & Wireshark tips & tricks

No new posts Tcpdump - dump HTTP headers as ASCII and HEX
View the latest post

Login

Username:   Password:   Log me on automatically each visit  

Statistics

Statistics

Total posts 617 | Total topics 987 | Total members 1192



News News Site map Site map SitemapIndex SitemapIndex RSS Feed RSS Feed Channel list Channel list


Delete all board cookies | The team | All times are UTC - 5 hours [ DST ]



phpBB SEO