Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Saving displayed output from terminal to file with a nice format

Tags:

regex

linux

bash

I need to "dump" all information from different commands to a file in bash. When I enter the command at the terminal it shows with a pretty nice formatted and is readable. For example I have this command

ifconfig -a | grep  "inet\|lo\|eth\|wlan"

and I want that output in a file I do this

echo $(ifconfig -a | grep  "inet\|lo\|eth\|wlan") >> filename.txt

but when I open filename.txt everything is like this

eth0 Link encap:Ethernet HWaddr # inet addr:#Bcast:# Mask:# inet6 addr: # Scope:Link eth0:1 Link encap:Ethernet HWaddr # inet addr:127.0.0.1 Bcast:127.255.255.255 Mask:255.0.0.0 lo Link encap:Local Loopback inet addr:127.0.0.1 Mask:255.0.0.0 inet6 addr: ::1/128 Scope:Host

but I want it to look like the terminal output something like this

eth0 Link encap:Ethernet 
HWaddr # 
inet addr:# 
Bcast:#
Mask:# 
inet6 addr: #
Scope:Link 
eth0:1 Link encap:Ethernet 
HWaddr # 
inet addr:127.0.0.1 
Bcast:127.255.255.255 
Mask:255.0.0.0 lo Link 
encap:Local 
Loopback inet addr:127.0.0.1 
Mask:255.0.0.0 
inet6 addr: ::1/128 Scope:Host

is it possible? I've been trying to replace "space" with \n with a sed command for example sed "s/ /\n/#" where # represent what space to remove, but this is clearly not the way to go. There must be another way to what I want. Its not only ifconfig command I need to have formatted.

like image 753
frallan123 Avatar asked Jan 26 '26 00:01

frallan123


1 Answers

What you are doing can be done simply with:

ifconfig -a | grep  "inet\|lo\|eth\|wlan" >> filename.txt

or

grep  "inet\|lo\|eth\|wlan" <<<"$(ifconfig -a)" >> filename.txt

And the reason for the "different" output in your current code is because all the whitespaces are lost. If you do:

echo "$(ifconfig -a | grep  "inet\|lo\|eth\|wlan")" >> filename.txt

you wouldn't have that problem.

like image 60
P.P Avatar answered Jan 27 '26 15:01

P.P



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!