Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

remove blank and/or extra tabs and append

Status
Not open for further replies.

paublo

ISP
Sep 14, 2006
127
US
Hi i have a file that consists of virtual domain email aliases. I need to have entries in this file be formatted like:

user1@virtualdomain.com <single tab> localuser<@appended_domain.com>

the problem is the current file consists or multiple tabs between the virtauldomain part and the localuser part and sometimes its just a random number of spaces not a tab. The other problem is the localuser part sometimes has domains appended to it and i would like those to stay but append a default domain to the localuser's that don't have a @something after it already. I imagine you can do this with awk and sed in a bash script but my skills with those commands are that good.

I appreciate any help.

thanks, paul
 
I have this piece of code as a common script I call to remove comments, leading/trailing white-space, redundant white-space, and blank lines.
white-space includes spaces, tabs, and carriage-return (octal 15)

Code:
whitespace=$(echo "\t\015 ")

sed -e 's/#.*$//'                             \
    -e "s/^[$whitespace]*//g"                 \
    -e "s/[$whitespace]*$//g"                 \
    -e "s/[$whitespace][$whitespace]*/ /g"    \
    -e "/^$/d"


 
This
Code:
sed -i 's/[\t ]\+/\t/g' file
will remove additional spaces/tabs, and transform a single space to a tab.

For the second part: "sometimes has domains appended to it" - how does that look like. A Small, maybe 4-line example without domains appended and with more or less appended. How the input could look like, how the result schould look like.

Note: If those domains are delimited by a space, this too will be replaced with a tab.

don't visit my homepage:
 
thanks guys for the replies, great info.

stefanwager that's exactly what I wanted the second part looks like this

jay@aawxx.com aawxx09
julie@aawxx.com aawxx10
cary@aawxx.com aawixx11
john@aawxx.com aawill13
sandy@aawxx.com sandyxxx@msn.com
postmaster@aawxx.com aawxx01
@aawxx.com aawxx01
 
Code:
echo "
jay@aawxx.com                          aawxx09
julie@aawxx.com                        aawxx10
cary@aawxx.com                         aawixx11
john@aawxx.com                         aawill13
sandy@aawxx.com                        sandyxxx@msn.com
postmaster@aawxx.com                   aawxx01
@aawxx.com                             aawxx01" | sed 's/\([\t ][a-z0-9]\+\)$/\t\1@default.org/' 

jay@aawxx.com                         	 aawxx09@default.org
julie@aawxx.com                       	 aawxx10@default.org
cary@aawxx.com                        	 aawixx11@default.org
john@aawxx.com                        	 aawill13@default.org
sandy@aawxx.com                        sandyxxx@msn.com
postmaster@aawxx.com                  	 aawxx01@default.org
@aawxx.com                            	 aawxx01@default.org
Since there is again space in the example, I used [\t ], but as a second command for sed, you will just use \t.

Code:
sed -i 's/[\t ]\+/\t/g;s/\([\t ][a-z0-9]\+\)$/\t\1@default.org/' file

don't visit my homepage:
 
stefan that works beautiful i have done some stuff with sed in the past but its not something i use all the time and when i need to use it its a one time thing. thanks for your help.

paul
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top