LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
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


Reply
  Search this Thread
Old 04-26-2010, 01:50 AM   #1
jeesun
Member
 
Registered: Aug 2007
Location: Australia
Distribution: RedHat Enterprise
Posts: 82

Rep: Reputation: 15
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
 
Old 04-26-2010, 02:54 AM   #2
centosboy
Senior Member
 
Registered: May 2009
Location: london
Distribution: centos5
Posts: 1,137

Rep: Reputation: 116Reputation: 116
Quote:
Originally Posted by jeesun View Post
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/
 
Old 04-26-2010, 03:35 AM   #3
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
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.
 
Old 04-26-2010, 03:54 AM   #4
jeesun
Member
 
Registered: Aug 2007
Location: Australia
Distribution: RedHat Enterprise
Posts: 82

Original Poster
Rep: Reputation: 15
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
 
Old 04-26-2010, 04:50 AM   #5
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
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
 
Old 04-26-2010, 05:27 AM   #6
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Quote:
Originally Posted by jeesun View Post
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Read text file column by column RVF16 Programming 11 05-31-2009 07:16 AM
how to append columns form a column file in another file adam_blackice Programming 4 09-17-2007 11:33 PM
Reading From a Column in a File jimmy512 Linux - Newbie 3 07-16-2007 11:49 PM
how to delete different column in a file? Tuku Programming 3 01-04-2007 09:53 AM
how to delete different column in a file? Tuku Linux - Newbie 2 01-04-2007 12:47 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 12:40 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration