LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   help with printing data (https://www.linuxquestions.org/questions/linux-newbie-8/help-with-printing-data-4175440617/)

NickPats 12-08-2012 06:27 PM

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.

rigor 12-08-2012 10:36 PM

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.

towheedm 12-08-2012 11:16 PM

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.

rigor 12-09-2012 11:45 AM

Oh, print, not print. ;)

rigor 12-09-2012 12:14 PM

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!

grail 12-09-2012 07:12 PM

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

NickPats 12-10-2012 02:09 PM

yeah i get it but actually according to question you are supposed to ask a number

grail 12-11-2012 01:45 AM

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.


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