text processing in os

Upload: hardik-darji

Post on 09-Apr-2018

221 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/7/2019 Text Processing in OS

    1/7

    Text Processing :-Cut :-

    To extract data from each line of a text file

    can be used for a file that contains data records so that each line consist of one or

    more fields separated by tab characters.

    Flag :-

    -c characterlist specify a list of characters to be cut from each

    line

    -f fieldlist Specify a list of fields to be cut from each line.

    -d character To override the character to be interpreted

    as the field delemiton.

    -s To suppress lines that do not hr the specified

    delimiter character.

    Ex :-

    1 If you want to extract the first field,cut f1 file1

    2 if you want to cut the chars 2 to 6 ,cut c2 -5 file1

    3 if you want to cut all chars in the first field up to the first 5 character, use thefollowing command.

    4 Cut ds s f1 file1

    ex : invokes the ex editor to edit one or more files

    fmt : can be used to format files to a 72 characters line by default.

    - preserves the blank lines in the i/p file as well as the spacing between words.- U can modify the line length using the width flag

    Fold : can be used to generate multiple lines from a single line by splitting the line at the

    specified position

  • 8/7/2019 Text Processing in OS

    2/7

    - By def, the line length is 80 bytes.

    Flag :

    -b specify the position in bytes.

    -s split a line after the last space at a position that is less than or equal to the

    specified width

    -w width specify the line width.

    Ex : if you want to split the line at byte position 40

    fold -w 40 file1 > file2;

    if you do not want to split words,

    fold w 40 s file1>file2.

    Join :- can be used to merge 2 files (one can be std. Input to execute a third file (Which can be

    std. O/p)

    - each line in the file is merged on the basis of a field that has the same value in bothi/p files to create one line in the o/p file.

    - The filed in each file are separated by either a space or the tab character.

    Flag :-- -1 field or j1 field specify that the join should be made on the basis of

    field in the first file.

    - -2 field or j2 field specify that the join should be made on the basisof the field in the second file.

    - -e string specify that blank fields in the o/p file bereplaced by the specified string.

    - -o field.fieldnumber specify that the o/p should consist of the specifiedfields.

    u can specify multiple fields by separating them

    with commands

    - -t char Modify the field seperator char from the default

    value of the space.

  • 8/7/2019 Text Processing in OS

    3/7

    Ex :-

    1 If u want to join the 2 files & display the matching lines as well as theNon-matching lines from the specified file.

    - Join a1 file1 file2

    paste :-

    can be used to paste lines from one or more files(one of them can be std. Input)

    to the std. O/p, which can be redirected to a file.

    - This comm. concatenates the line from each input file to the output file, separating

    them by default with the tab character.

    Flag :-

    -d list specify chars, that will be used to separate corresponding lines from

    the i/p files in the o/p files.

    u can specify multiple chars if u have multiple i/p files.-s merge subsequent lines from the i/p file for each i/p file, one at a time,

    separated by the specified delimiter char.

    Ex :-

    - if u want to modify def. Separate filename tab to / =>past -d / file1 file2

    Sort :-

    Is used to sort one or more files in the specified order by the specified key.

  • 8/7/2019 Text Processing in OS

    4/7

    It can also be used to merge files that hr already been sorted.

    Flag :-

    - -k key specify the key on which to sort the specification for the key

    inchides the starting field & column position & the end field

    & col. Position.

    - -A Specify the sorting be done according to ASCII sequence.

    - -c check whether the specified files are sorted according to the

    specified key & order.

    - -d sort according to dictionary order.

    - -f change all letters to uppercase before the sort.

    - -i Ignore nondisplayable characters. For comparison.

    - -m merge presorted of files.

    - -n sort according to numerical valued.

    - -o file redirect the o/p to the specified file instead of to the std. O/p

    - -r sort the o/p in the reverse order of the specified order.

    - -u create only one line in the o/p for lines that sort identically.

    Ex :-

    1 if you want to sort in the reverse order,sort r file1

    2 if you want to sort according to alphabetic ordersort d file1

    tr :-

    to translate or delete characters from std. I/p to generate std. O/p./

    - translate specified chars in the i/p from the input to generate the output.- Delete specified chars in the i/p from the input to generate the output.- Delete all but the first occurrences of the specified chars.

  • 8/7/2019 Text Processing in OS

    5/7

    Flag :-

    -c translate all but the specified chars using the specified new

    characters.

    -d delete the specified chars.

    -s delete all but the first occurrence of the specified chars.

    Specify i/p & o/p sequence of chars

    [char1-char2] to specify a range of chars. Including char1 & char2

    [char1*number] to specify number occurrences of char.

    [char *] to specify the use of as many occurrences as are needed

    of character so that the i/p string of chars. To be

    translated matches the o/p chars.

    [:charlist:] to specify a list of chars. As the i/p or o/p string.

    The charlist can be upper, lower, alph, space, digits,

    & so on.

    Ex :-

    1 if u want to change the double quotes to spaces,tr \ < file1

    2 if u want to change all lowercase letters to uppercase lettertr [:lower:] [:upper:]

  • 8/7/2019 Text Processing in OS

    6/7

    Can be used to eliminate duplicate adjacent lines from a file or from std. I/p to

    Generate std o/p or another file.

    - Also it is possible to compare only part of a line for comparison by usingCertain flags.

    Flag :-

    -c precede each line with a number while displaying the o/p (the no.

    specifies the no of recursences of the line in the i/p file).

    -d display only th elines that occur multiple times adjacent to each other in

    the i/p file.

    -u display only the lines that appears only once in the i/p file.

    -s no. of characters

    or

    +noofchars specify the no of chars from the start of a line that will be ignored

    while comparing adjacent lines.

    -no of fields

    or

    +no of fields specify the no of fields from the start of a line that will be ignored

    while comparing adjacent lines

    Ex :-

    1 if u want to find unique lines in file12 if u want to display only the duplicate line

    uniq d file1

    3 if u want to display the lines that appear only once in file1uniq u file1

    Sed :-

    To edit a file using a script

  • 8/7/2019 Text Processing in OS

    7/7

    - in the script, u can specify commands to edit one or more lines according rulesspecified as part of one or more commands.

    Flag :-

    -e command use the specified sed comm.. to edit the file.

    -f filename use the filename as the editing script to edit the file.

    -n suppress messages from sed.

    Ex :-

    1 if u want to print the line no of the line in which a specified pattern is found,sed e /sed/= file1