I have a folder containing text files. I need to extract specific lines from the files of this folder based on another file input.txt. How can I do this with awk/sed?
Hi Rudic,
Could you please explain the code. Not sure what i got is all perfect. FNR==NR ## just compare the number of records of two files. While reading first file the condition would be true. So it will jump to just next block. {if (/^\*/) ## search for pattern starting with asterisk , that i found no where so what is the purpose. ? {TFN = substr ($0,2) ## for all fields starting 2 characters will be assigned to variable TFN SAM[TFN] = "-" ## Associative array would be declared and assiged value "-" to them. SAM[TFN] = SAM[TFN] $0 "-" ## if the IF block become false then add "-" after each record.
awk '
# start of code block for only first file at command line
FNR==NR {if (/^\*/) {TFN = substr ($0,2) # remove the * from the file name we want to structure, save in a Temporal File Name variable
SAM[TFN] = "-" # start by adding a marker ("-") for separation identification
next # skip to next line of first file at command line, ignore the remaining code blocks
}
# this expression is for the lines that contains not a filename denotation
SAM[TFN] = SAM[TFN] $0 "-" # append the whole record to the current id key; followed by separator markers
next # skip to next line in same file, ignore the rest
}
# end of block for first file at command line
# the following block applies only to the rest of the files in the command line, not the first
SAM[FILENAME] ~ "-" $3 "-" {print > FILENAME ".res"} # look for the FILENAME (current file being processed), in the previously built data structure
# and see if it can match the pattern created by the concatenation of "-" $3 "-" (example: "-10-" ); if it does send the current $0 to the file FILENAME.res