LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 09-28-2009, 01:39 AM   #1
seb357
LQ Newbie
 
Registered: Sep 2009
Posts: 4

Rep: Reputation: 0
Parsing columns in bash


Hi,

as a result of a find command, i have

852065 72: /bin/gunzip
852065 72: /bin/gzip
852065 72: /bin/zcat


(the first column is inode number and the second is size if you're curious)

I want to be able to format it in a way such that:
852065 72:
/bin/gunzip
/bin/gzip
/bin/zcat

I know I can get the bottom half using awk- but I can't figure out how to extract the first set of numbers and get them to appear only once

any help?

Thanks!
 
Old 09-28-2009, 01:52 AM   #2
lutusp
Member
 
Registered: Sep 2009
Distribution: Fedora
Posts: 835

Rep: Reputation: 102Reputation: 102
Quote:
Originally Posted by seb357 View Post
Hi,

as a result of a find command, i have

852065 72: /bin/gunzip
852065 72: /bin/gzip
852065 72: /bin/zcat


(the first column is inode number and the second is size if you're curious)

I want to be able to format it in a way such that:
852065 72:
/bin/gunzip
/bin/gzip
/bin/zcat

I know I can get the bottom half using awk- but I can't figure out how to extract the first set of numbers and get them to appear only once

any help?

Thanks!
You aren't explaining it very well. Do the first two numbers ever change? Do you have a longer example from the data source? Without that I am only guessing about what might work.

Also, do you really think you can list multiple files, all with the same inode number? That's not how inode numbers work.

Pretend that the information matters to the solution (because it does). Explain the problem properly -- otherwise there is no chance to solve it.
 
Old 09-28-2009, 01:55 AM   #3
seb357
LQ Newbie
 
Registered: Sep 2009
Posts: 4

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by lutusp View Post
You aren't explaining it very well. Do the first two numbers ever change? Do you have a longer example from the data source? Without that I am only guessing about what might work.

Also, do you really think you can list multiple files, all with the same inode number? That's not how inode numbers work.

Pretend that the information matters to the solution (because it does). Explain the problem properly -- otherwise there is no chance to solve it.

The first numbers change depending on the directory:

for example, if i execute

find /bin ! -type d -links +2 -ls 2>/dev/null | sort -g | awk '{ print $1, $2":", $11 }'

then the result above appears

My issue is mainly with formatting, i'm just new to using bash
 
Old 09-28-2009, 02:02 AM   #4
lutusp
Member
 
Registered: Sep 2009
Distribution: Fedora
Posts: 835

Rep: Reputation: 102Reputation: 102
Quote:
Originally Posted by seb357 View Post
The first numbers change depending on the directory:

for example, if i execute

find /bin ! -type d -links +2 -ls 2>/dev/null | sort -g | awk '{ print $1, $2":", $11 }'

then the result above appears

My issue is mainly with formatting, i'm just new to using bash
Don't post your idea for a solution, instead describe the problem to be solved. Be detailed and specific.
 
Old 09-28-2009, 02:54 AM   #5
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
Quote:
Originally Posted by seb357 View Post
Hi,

as a result of a find command, i have

852065 72: /bin/gunzip
852065 72: /bin/gzip
852065 72: /bin/zcat


(the first column is inode number and the second is size if you're curious)

I want to be able to format it in a way such that:
852065 72:
/bin/gunzip
/bin/gzip
/bin/zcat

I know I can get the bottom half using awk- but I can't figure out how to extract the first set of numbers and get them to appear only once

any help?

Thanks!
As others have said, it's not very clear ... but
does the snippet of awk do what you want?
save to script.awk, rund like
find .... | awk -f script.awk
Code:
BEGIN{
        FS=":"
        hold=""
}
{
        if ( hold != $1 ){print $1}
        if( NR > 2 && hold == $1 ){
                print $2
        } else { 
                print $2
                hold = $1
        }
}


Cheers,
Tink
 
Old 09-28-2009, 03:22 AM   #6
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
Quote:
Originally Posted by lutusp View Post
Don't post your idea for a solution, instead describe the problem to be solved. Be detailed and specific.
Don't we usually like people to post both their problem and their attempt at a solution? I fully agree that questions simply asking how to make a solution work can be unproductive because the solution may be sub-optimal, even unworkable. In this case the solution attempt does clarify that the problem really is about files with multiple hard links -- and that's useful because it is increasingly unusual.
 
Old 09-28-2009, 09:21 AM   #7
seb357
LQ Newbie
 
Registered: Sep 2009
Posts: 4

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by catkin View Post
Don't we usually like people to post both their problem and their attempt at a solution? I fully agree that questions simply asking how to make a solution work can be unproductive because the solution may be sub-optimal, even unworkable. In this case the solution attempt does clarify that the problem really is about files with multiple hard links -- and that's useful because it is increasingly unusual.
I'm trying to be as general as possible

say you have a long listing:

ls -l

-rw-r--r-- 2 seb357 faculty 1215 Mar 25 2009 banana
-rw-r--r-- 1 seb357 faculty 443 Sep 27 18:06 chkopts.bash
-rw------- 1 seb357 faculty 358 Mar 31 2008 dead.letter
drwxr-sr-x 3 seb357 faculty 4096 Apr 13 16:28 gasudoku


I want to be able to capture a single cell from that table, so i could do something like making a variable named user = seb357
 
Old 09-28-2009, 12:58 PM   #8
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
Quote:
Originally Posted by seb357 View Post
I'm trying to be as general as possible
If you really want to be as generic as possible (= portable to various *n*x versions) then you don't want to use ls to generate a list of files or ls -l to get information about them for the reasons explained here.

If "as general as possible" is on a version of *n*x that produces ls -l output in the same format as on your current version and only operates on files with names that ls can display then
Code:
#!/bin/bash
shopt -s extglob
IFS='
' 
for line in $(/bin/ls -l)
do
    unset IFS  # Equivalent to setting it back to the default 
    array=( $line )
    [[ "${array[0]}" == 'total' ]] && continue  # Skip headng
    perms="${array[0]}"
    link_count="${array[1]}"
    user="${array[2]}"
    filename="${line##*:*([0-9]) }"
    echo "\$perms is '$perms'"
    echo "\$link_count is '$link_count'"
    echo "\$user is '$user'"
    echo "\$filename is '$filename'"
done
 
Old 09-28-2009, 03:37 PM   #9
H_TeXMeX_H
LQ Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1285Reputation: 1285Reputation: 1285Reputation: 1285Reputation: 1285Reputation: 1285Reputation: 1285Reputation: 1285Reputation: 1285
For example 1:

The simplest way I can think of to get the output above from the input above is:

Code:
find ... | awk '{ if (NR == 1) printf("%s %s\n%s\n", $1, $2, $3); else print $3 }'
That's of course assuming that the first two columns are always the same.

For example 2:

Code:
bash-3.1$ var=$(ls -l | cut -d' ' -f4 | head -n1)
bash-3.1$ echo $var
faculty
 
Old 09-28-2009, 03:40 PM   #10
seb357
LQ Newbie
 
Registered: Sep 2009
Posts: 4

Original Poster
Rep: Reputation: 0
haha, as generic as possible for my two systems

Thank you a lot catkin, that's exactly what i needed
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash script sorting multple columns together KBriggs Linux - Newbie 2 08-13-2009 01:59 PM
Selecting certain parts of a list of columns in BASH mikejreading Linux - Newbie 6 05-07-2009 05:48 AM
sorting columns in bash twistadias Linux - Newbie 8 08-25-2008 01:22 AM
Bash - How can you list 2 arrays in columns zak111 Programming 12 07-23-2007 12:27 PM
bash thinks term is 80 columns? JurgyMan Linux - General 4 06-13-2006 10:14 AM


All times are GMT -5. The time now is 05:40 PM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration