Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.
Notices
Welcome to
LinuxQuestions.org , a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free.
Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please
contact us . If you need to reset your password,
click here .
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a
virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month.
Click here for more info.
04-26-2010, 01:50 AM
#1
Member
Registered: Aug 2007
Location: Australia
Distribution: RedHat Enterprise
Posts: 82
Rep:
File name in separate column
Hi
I have some series of files, which actually named bellow
<day_month_yr>_<hh:mm:ss>.<host_name>.<IP.ip.ip.ip>.<log_name>.txt
I want to show this file name below the column may be
Date Time Host_Name IP_address Login_Name
----- ---- --------- ---------- ----------
<day_month_yr> <hh:mm:ss> host_name x.x.x.x log_name
...
...
Can anyone help
04-26-2010, 02:54 AM
#2
Senior Member
Registered: May 2009
Location: london
Distribution: centos5
Posts: 1,137
Rep:
Quote:
Originally Posted by
jeesun
Hi
I have some series of files, which actually named bellow
<day_month_yr>_<hh:mm:ss>.<host_name>.<IP.ip.ip.ip>.<log_name>.txt
I want to show this file name below the column may be
Date Time Host_Name IP_address Login_Name
----- ---- --------- ---------- ----------
<day_month_yr> <hh:mm:ss> host_name x.x.x.x log_name
...
...
Can anyone help
Do you know perl? Format can do this and/or the printf command
See example here:
http://www.webreference.com/programming/perl/format/
04-26-2010, 03:35 AM
#3
LQ Veteran
Registered: Sep 2003
Posts: 10,532
Hi,
A sed solution:
sed 's/\([0-9][0-9_]*\)_\([0-9][0-9:]*\).\([a-z][a-z0-9]*\).\([1-9][0-9]*.[1-9][0-9]*.[1-9][0-9]*.[1-9][0-9]*\).\([a-z0-9][a-z0-9]*\)/\1 \2 \3 \4 \5/' infile
Example run:
Code:
cat infile
01_01_2001_01:01:01.foo.1.1.1.1.log1.txt
05_05_2005_05:05:05.bar.5.5.5.5.log5.txt
10_10_2010_10:10:10.foobar.10.10.10.10.log10.txt
sed 's/\([0-9][0-9_]*\)_\([0-9][0-9:]*\).\([a-z][a-z0-9]*\).\([1-9][0-9]*.[1-9][0-9]*.[1-9][0-9]*.[1-9][0-9]*\).\([a-z0-9][a-z0-9]*\)..*/\1 \2 \3 \4 \5/' blaat
01_01_2001 01:01:01 foo 1.1.1.1 log1
05_05_2005 05:05:05 bar 5.5.5.5 log5
10_10_2010 10:10:10 foobar 10.10.10.10 log10
Hope this helps.
04-26-2010, 03:54 AM
#4
Member
Registered: Aug 2007
Location: Australia
Distribution: RedHat Enterprise
Posts: 82
Original Poster
Rep:
Thanks druuna for your valuable reply.
But not the cat command to format file content.
suppose the file name is 01_01_2001_01:01:01.foo.1.1.1.1.log1.txt and I want to reformat it's name actually
04-26-2010, 04:50 AM
#5
LQ 5k Club
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Here's a pure bash solution. Some of the bash-isms are pretty dense so it's structured for legibility rather than minimalism
Code:
# File name general format
# <day_month_yr>_<hh:mm:ss>.<host_name>.<IP.ip.ip.ip>.<log_name>.txt
# Assume date and time are fixed length
# Column titles
readonly ct_date='Date'
readonly ct_time='Time'
readonly ct_hostname='Host_Name'
readonly ct_ip='IP_address'
readonly ct_login_name='Login_Name'
# Simulate reading file names into an array
filename[0]='01_01_2001_01:01:01.foo.1.1.1.1.log1.txt'
# Parse variable length parts of file names
for (( i=0; i<${#filename[*]}; i++ ))
do
buf=${filename[i]:20}
hostname[i]=${buf%%.*}
buf=${buf#*.}
buf=${buf%.txt}
login_name[i]=${buf##*.}
ip=${buf%.$login_name}
done
# Determine required colum widths
cw_date=10
cw_time=8
cw_hostname=${#ct_hostname}
cw_ip=${#ct_ip}
cw_login_name=${#ct_login_name}
for (( i=0; i<${#filename[*]}; i++ ))
do
[[ ${#hostname[i]} -gt ${#${cw_hostname[i]}} ]] && cw_hostname=${#hostname[i]}
[[ ${#login_name[i]} -gt ${#${cw_login_name[i]}} ]] && cw_login_name=${#login_name[i]}
[[ ${#ip[i]} -gt ${#${cw_ip[i]}} ]] && cw_ip=${#ip[i]}
done
# Build format string
fmt="| %${cw_date}s | %${cw_time}s | %${cw_hostname}s | %${cw_ip}s | %${cw_login_name}s |\n"
# Write table
printf "$fmt" "$ct_date" "$ct_time" "$ct_hostname" "$ct_ip" "$ct_login_name"
for (( i=0; i<${#filename[*]}; i++ ))
do
printf "$fmt" \
"${filename[i]:0:10}" \
"${filename[i]:11:8}" \
"${hostname[i]}" \
"${ip[i]}" \
"${login_name[i]}"
done
04-26-2010, 05:27 AM
#6
LQ Veteran
Registered: Sep 2003
Posts: 10,532
Quote:
Originally Posted by
jeesun
Thanks druuna for your valuable reply.
But not the cat command to format file content.
suppose the file name is 01_01_2001_01:01:01.foo.1.1.1.1.log1.txt and I want to reformat it's name actually
I really don't understand what you are trying to say.
Have a look at catkin's example, maybe that will do whatever it is you want.
All times are GMT -5. The time now is 12:40 AM .
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know .
Latest Threads
LQ News