LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 12-08-2012, 07:27 PM   #1
NickPats
Member
 
Registered: Oct 2012
Posts: 43

Rep: Reputation: Disabled
help with printing data


i have a data like

username1
number string
-file1
-file2
.
.
-filen

username2
number string
-file1
-file2
.
.
-filen



how can i just print files under specific usernames. i am asking user to enter username.
 
Old 12-08-2012, 11:36 PM   #2
kakaka
Member
 
Registered: Sep 2003
Posts: 382

Rep: Reputation: 86
In a multi-User environment, normally each User would just print their own files, without needing to specify their User name.

So please give us some understanding of the printing is set up on the system you are trying to use.
 
Old 12-09-2012, 12:16 AM   #3
towheedm
Member
 
Registered: Sep 2011
Location: Trinidad & Tobago
Distribution: Debian Squeeze
Posts: 585

Rep: Reputation: 119Reputation: 119
From http://www.catonmat.net/blog/sed-one...ned-part-two/:
Quote:
58. Print a paragraph that contains "AAA". (Paragraphs are separated by blank lines).

sed -e '/./{H;$!d;}' -e 'x;/AAA/!d;'

First notice that this one-liner is divided in two parts for clearness. The first part is "/./{H;$!d;}" and the second part is "x;/AAA/!d".

The first part has an interesting pattern match "/./". What do you think it does? Well, a line separating paragraphs would be a blank line, meaning it would not have any characters in it. This pattern matches only the lines that are not separating paragraphs. These lines get appended to hold buffer with "H" command. They also get prevented from printing with "d" command (except for the last line, when "d" does not get executed ("$!" restricts "d" to all but the last line)). Once sed sees a blank line, the "/./" pattern no longer matches and the second part of one-liner gets executed.

The second part exchanges the hold buffer with pattern space by using the "x" command. The pattern space now contains the whole paragraph of text. Next sed tests if the paragraph contains "AAA". If it does, sed does nothing which results in printing the paragraph. If the paragraph does not contain "AAA", sed executes the "d" command that deletes it without printing and restarts execution at first command.
Replace AAA with the username:
Code:
read -p "Please enter the username: " ; sed -e '/./{H;$!d;}' -e 'x;/$REPLY/!d;'
Will need to finetune it to remove the line immediately after the username.

Hope it helps.
 
Old 12-09-2012, 12:45 PM   #4
kakaka
Member
 
Registered: Sep 2003
Posts: 382

Rep: Reputation: 86
Oh, print, not print.
 
Old 12-09-2012, 01:14 PM   #5
kakaka
Member
 
Registered: Sep 2003
Posts: 382

Rep: Reputation: 86
In that case, if I put this data:

Code:
username1
number string
-user1_file1
-user1_file2
-user1_file3
-user1_filen_minus_one
-user1_filen

username2
number string
-user2_file1
-user2_file2
-user2_file3
-user2_file4
-user2_file5
-user2_filen_minus_one
-user2_filen
in a file named data.txt, and this Awk program code:

Code:
BEGIN {
        # Execute this code section only once when the program begins.

        # Initialize symbolic TRUE and FALSE constants:
        TRUE  =  (  1  ==  1  ) ;
        FALSE  =  (  1  ==  0  ) ;

        # Initialize control flags:
        found_user_name = FALSE ;
        in_file_name_list = FALSE ;
      }

/^.+$/  {
            # Found a non-blank line.

            if (  in_file_name_list  ==  TRUE  )
            {
                # Output the line
                print $0 ;

                # Skip to checking the next input line.
                next ;
            }

            if (  $0  ~  user_name  )
            {
                # The input line matches the User Name.
                found_user_name  =  TRUE ;

                # Skip to checking the next input line.
                next ;
            }

            if (  found_user_name  ==  TRUE  )
            {
                # Since we skipped this test when we first found the User Name,
                # the next line will begin the list of file names.
                in_file_name_list = TRUE ;

                # Skip to checking the next input line.
                next ;
            }

        }

/^$/  {
        # Found blank line.
        # We've reach the end of a file list.
        # Reset the control flags.

        found_user_name = FALSE ;
        in_file_name_list = FALSE ;
      }
in a file named show_file_names_by_username.awk

then run this sequence of commands, repeatedly, providing different User Names as input:

Code:
read -p 'Please enter the User Name: ' user_name ;  gawk -vuser_name="$user_name"  -f  show_file_names_by_username.awk <  data.txt
this is the output I get with the corresponding inputs:

Code:
Please enter the User Name: username1
-user1_file1
-user1_file2
-user1_file3
-user1_filen_minus_one
-user1_filen
Code:
Please enter the User Name: username2
-user2_file1
-user2_file2
-user2_file3
-user2_file4
-user2_file5
-user2_filen_minus_one
-user2_filen
Code:
Please enter the User Name: username3
Is that about what you need?

Hope this Helps!
 
Old 12-09-2012, 08:12 PM   #6
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,698

Rep: Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988
hmmmm ... I think kakaka is on the right track although I think it could be a little simpler:
Code:
awk 'BEGIN{ FS="\n";RS="^$\n"; printf "Please enter the User Name: "; getline uname < "-"}$1 ~ uname' file
 
Old 12-10-2012, 03:09 PM   #7
NickPats
Member
 
Registered: Oct 2012
Posts: 43

Original Poster
Rep: Reputation: Disabled
yeah i get it but actually according to question you are supposed to ask a number
 
Old 12-11-2012, 02:45 AM   #8
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,698

Rep: Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988
Again you refer to this 'question' and if you do not spell out where this is coming from, everyone will assume this is homework and as per the LQ rules, we do not do homework.
 
  


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
YE-DATA printer Model No: YD-4800 is not printing properly on SLES 11 vm_devadas Linux - Hardware 0 07-13-2012 06:26 AM
printing data in an ip packet Anumathew Linux - Newbie 2 10-06-2010 02:56 AM
perl not printing %d data knockout_artist Programming 2 11-07-2008 08:33 PM
printing data from a comment line. liguorir Linux - Software 3 07-13-2005 02:11 PM
WP for cmd-line data merging & printing? MikHud Linux - Software 0 11-06-2002 03:36 AM


All times are GMT -5. The time now is 01:04 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration