/media/bill/Dell2/Website - raw/System_maintenance/Linux/awk notes.txt www.BillHowell.ca 10Jun2021 initial gawk = GNU awk I will use QNial - awk is too limited for TD transactio, plus I can generalise to all bank accounts 24************************24 #] #] ********************* #] "$d_web"'??? notes.txt' # www.BillHowell.ca 10Sep2023 initial # view in text editor, using constant-width font (eg courier), tabWidth = 3 #48************************************************48 #24************************24 # Table of Contents, generate with : # $ grep "^#]" "$d_web"'??? notes.txt' | sed "s/^#\]/ /" # #24************************24 # Setup, ToDos, #08********08 #] ??Sep2023 #08********08 #] ??Sep2023 #08********08 #] ??Sep2023 #08********08 #] ??Sep2023 #08********08 #] ??Sep2023 #08********08 #] 10Sep2023 awk pick lines in file by index for me, example to get first line of an html file : $ awk ... oops, I have to learn to set up awk for this simple example, just grep "str" -with-line-number "$pinn" +-----+ https://stackoverflow.com/questions/2017034/how-do-i-extract-lines-from-a-file-using-their-line-number-on-unix How do I extract lines from a file using their line number on unix? Asked 13 years, 8 months ago Modified 13 years, 5 months ago Viewed 19k times +--+ with awk it's as simple as: awk 'NR==1 || NR==5 || NR==1010' "file" edited Mar 18, 2010 at 23:15 Dennis Williamson answered Jan 6, 2010 at 23:10 ennuikiller 08********08 #] 10Jun2021 awk extract records +-----+ https://askubuntu.com/questions/439732/how-can-i-process-multi-line-records-with-awk-in-a-bash-script $ awk '$2=="KFC" {print; for(i=1; i<=4; i++) { getline; print}}' example.txt Restaurant: KFC City: NYC State: NY Address: 123 Madison Square Phone: 911 The above command will get and print the consecutive 4 lines along with the current line because it was fed into a for loop.The search pattern $2=="KFC" will helps to get a particular line from the multiple lines. edited Mar 27 '14 at 4:33 answered Mar 27 '14 at 4:26 Avinash Raj +--+ Using sed: $ sed -n '/KFC/,/^$/p' file Restaurant: KFC City: NYC State: NY Address: 123 Madison Square Phone: 911 $ sed -n '/McDo/,/^$/p' file Restaurant: McDonalds City: Miami State: Florida Address: 123 Biscayne Blvd Phone: 911 Explanation This is basic sed function, you can refer USEFUL ONE-LINE SCRIPTS FOR SED # print section of file between two regular expressions (inclusive) sed -n '/Iowa/,/Montana/p' # case sensitive edited Apr 27 '17 at 8:33 d a i s y 4,95399 gold badges3535 silver badges5656 bronze badges answered Mar 27 '14 at 4:22 BMW +-----+ https://www.computerhope.com/unix/gawk.htm Linux gawk command Updated: 03/13/2021 by Computer Hope Official gawk user's guide If you want to learn more about this incredibly powerful language, check out the GNU gawk user guide. https://www.gnu.org/software/gawk/manual/gawk.html +-----+ https://www.gnu.org/software/gawk/manual/html_node/Multiple-Line.html Another way to separate fields is to put each field on a separate line: to do this, just set the variable FS to the string "\n". (This single-character separator matches a single newline.) A practical example of a data file organized this way might be a mailing list, where blank lines separate the entries. Consider a mailing list in a file named addresses, which looks like this: Jane Doe 123 Main Street Anywhere, SE 12345-6789 John Smith 456 Tree-lined Avenue Smallville, MW 98765-4321 … A simple program to process this file is as follows: # addrs.awk --- simple mailing list program # Records are separated by blank lines. # Each line is one field. BEGIN { RS = "" ; FS = "\n" } { print "Name is:", $1 print "Address is:", $2 print "City and State are:", $3 print "" } Running the program produces the following output: $ awk -f addrs.awk addresses -| Name is: Jane Doe -| Address is: 123 Main Street -| City and State are: Anywhere, SE 12345-6789 -| -| Name is: John Smith -| Address is: 456 Tree-lined Avenue -| City and State are: Smallville, MW 98765-4321 -| … See section Printing Mailing Labels for a more realistic program dealing with address lists. The following list summarizes how records are split, based on the value of RS: RS == "\n" Records are separated by the newline character (‘\n’). In effect, every line in the data file is a separate record, including blank lines. This is the default. RS == any single character Records are separated by each occurrence of the character. Multiple successive occurrences delimit empty records. RS == "" Records are separated by runs of blank lines. When FS is a single character, then the newline character always serves as a field separator, in addition to whatever value FS may have. Leading and trailing newlines in a file are ignored. RS == regexp Records are separated by occurrences of characters that match regexp. Leading and trailing matches of regexp delimit empty records. (This is a gawk extension; it is not specified by the POSIX standard.) If not in compatibility mode (see section Command-Line Options), gawk sets RT to the input text that matched the value specified by RS. But if the input file ended without any text that matches RS, then gawk sets RT to the null string. +-----+ https://stackoverflow.com/questions/14350856/can-awk-patterns-match-multiple-lines From pcregrep(1): -M, --multiline Allow patterns to match more than one line. When this option is given, patterns may usefully contain literal newline characters and internal occurrences of ^ and $ characters. The output for a successful match may consist of more than one line, the last of which is the one in which the match ended. If the matched string ends with a newline sequence the output ends at the end of that line. When this option is set, the PCRE library is called in “multiline” mode. There is a limit to the number of lines that can be matched, imposed by the way that pcregrep buffers the input file as it scans it. However, pcregrep ensures that at least 8K characters or the rest of the document (whichever is the shorter) are available for forward matching, and similarly the previous 8K characters (or all the previous characters, if fewer than 8K) are guaranteed to be available for lookbehind assertions. This option does not work when input is read line by line (see --line-buffered.) Share Follow answered Jan 16 '13 at 3:38 Cong Wang +-----+ https://www.geeksforgeeks.org/gawk-command-in-linux-with-examples/ gawk command in Linux with Examples Last Updated : 06 May, 2019 gawk command in Linux is used for pattern scanning and processing language. The awk command requires no compiling and allows the user to use variables, numeric functions, string functions, and logical operators. It is a utility that enables programmers to write tiny and effective programs in the form of statements that define text patterns that are to be searched for, in a text document and the action that is to be taken when a match is found within a line. gawk command can be used to : Scans a file line by line. Splits each input line into fields. Compares input line/fields to pattern. Performs action(s) on matched lines. Transform data files. Produce formatted reports. Format output lines. Arithmetic and string operations. Conditionals and loops. Some Important Options: -f progfile, –file=progfile: Read the AWK program source from the file program-file, instead of from the first command line argument. Multiple -f (or –file) options may be used. -F fs, –field-separator=fs: It uses FS for the input field separator (the value of the FS predefined variable). -v var=val, –assign=var=val: Assign the value val to the variable var, before execution of the program begins. Some Built In Variables: NR: It keeps a current count of the number of input line. NF: It keeps a count of the number of fields within the current input record. FS: It contains the field separator character which is used to divide fields on the input line. RS: It stores the current record separator character. OFS: It stores the output field separator, which separates the fields when Awk prints them. ORS: It stores the output record separator, which separates the output lines when Awk prints them. Article Contributed By : Mandeep_Sheoran # enddoc