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!