Sponsored Content
Top Forums Shell Programming and Scripting How to understand special character for line reading in bash shell? Post 303039063 by RudiC on Saturday 21st of September 2019 02:24:23 PM
Old 09-21-2019
Not sure I'm an expert nor a guru, but this is what I'd do: refer to the respective man pages.
man bash:
Quote:
Parameter Expansion

${parameter#word}
${parameter##word}

Remove matching prefix pattern.


${parameter%word}
${parameter%%word}

Remove matching suffix pattern.



CONDITIONAL EXPRESSIONS.



An additional binary operator, =~, is available, with the same precedence as == and !=. When it is used, the string to the right of the operator is considered a POSIX extended regular expression and matched accordingly (as in regex(3))
Of course you need to learn the difference between shell's pattern matching and regex matching.

man regex:
Quote:
An atom is a regular expression ... '^' (matching the null string at the beginning of a line)

A bracket expression is a list of characters enclosed in "[]". It normally matches any single character from the list (but see below). If the list begins with '^', it matches any single character (but see below) not from the rest of the list.
Admittedly the ambiguous use of the caret is something you need to accustom to.
These 2 Users Gave Thanks to RudiC For This Post:
 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

[OpenServer 5]Line Printing and special character (é @)

Hello, On Sco OpenServer 5, i want to print using the lpr command, no CUPS installed. I print on an HP LaserJet 4050 on LAN (IP 192.168.x.x) the printer is installed by HP Network Printer service. it works fine, but Specials characters, like é, @ or ° print bad characters. Is there... (5 Replies)
Discussion started by: tankd
5 Replies

2. Shell Programming and Scripting

bash shell: 'exec', 'eval', 'source' - looking for help to understand

Hi, experts. Whould anybody clear explay me difference and usage of these 3 commands (particulary in bash) : exec eval source I've tryed to read the manual pages but did not get much. Also could not get something useful from Google search - just so much and so not exactly, that is... (3 Replies)
Discussion started by: alex_5161
3 Replies

3. Shell Programming and Scripting

Adding a special character at the end of the line

I used following to add * at the end of the line in file1. It adds * at the end but has a space before it for some lines but some other lines it adds exactly after the last character. How do I take out the space ? sed 's/$/*/' file1 > file2 example: contents of file1 : ... (2 Replies)
Discussion started by: pitagi
2 Replies

4. Shell Programming and Scripting

How to insert a character in line in Special condition?

Hi, I have a log file generated by a tool which has the following look : /tmp/releases/directory/datefilename1_release_date.zip /tmp/releases/directory/datefilename2_release_date.zip /tmp/releases/directory/datefilename3_release_date.zip... (8 Replies)
Discussion started by: bhaskar_m
8 Replies

5. Shell Programming and Scripting

I need to understand the differences between the bash shell and the Bourne shell

I do not claim to be an expert, but I have done things with scripts that whole teams of folks have said can not be done. Of course they should have said we do not have the intestinal fortitude to git-r-done. I have been using UNIX actually HPUX since 1992. Unfortunately my old computer died and... (7 Replies)
Discussion started by: awk_sed_hello
7 Replies

6. Shell Programming and Scripting

Printing special character in bash

I am using this character as a delimiter 'þ' Currently, I set it straight: DELIMITER='þ' However, while copying the file, this character often gets mangled. Is there a bash way (perhaps using tr or printf) of generating this character. It corresponds to "chr(0xfe)" if using perl. (I've... (6 Replies)
Discussion started by: sentinel
6 Replies

7. Shell Programming and Scripting

parse special character in the line

Hi all, I have a file with some module names as below. Font::AFM Data::Grove ---> libxml-perl Net::LDAP ---> perl-ldap DBI XML .... ... .... and so on ... The file has some lines with the character " -->" . Now how can I cut only the last column of the line wherever "-->" is... (4 Replies)
Discussion started by: vijaya2006
4 Replies

8. UNIX for Dummies Questions & Answers

Reading character by character - BASH

Hello every one and thanks in advance for the time you will take to think about my problem. I would like to know if it's possible (in BASH) to read a text file character after character. Exactly this is what I would like to do : Txt file : ATGCAGTTCATTGCCAAA...... (~2.5 millions... (3 Replies)
Discussion started by: sluvah
3 Replies

9. Shell Programming and Scripting

Shell script question in special character

when I execute the unix commands its works normally in the 1st part. When I the tried the same in shell scripting the directory is not displayed in 2nd part example. please let me know what needs to be done. Unix : client=~zsvdbs echo $client /shome/zsvhome/zsvdbs Using... (3 Replies)
Discussion started by: keerthi2016
3 Replies

10. UNIX for Beginners Questions & Answers

Escape bash-special character in a bash string

Hi, I am new in bash scripting. In my work, I provide support to several users and when I connect to their computers I use the same admin and password, so I am trying to create a script that will only ask me for the IP address and then connect to the computer without having me to type the user... (5 Replies)
Discussion started by: arcoa05
5 Replies
RE_FORMAT(7)					       BSD Miscellaneous Information Manual					      RE_FORMAT(7)

NAME
re_format -- POSIX 1003.2 regular expressions DESCRIPTION
Regular expressions (``REs''), as defined in IEEE Std 1003.2 (``POSIX.2''), come in two forms: modern REs (roughly those of egrep(1); 1003.2 calls these ``extended'' REs) and obsolete REs (roughly those of ed(1); 1003.2 ``basic'' REs). Obsolete REs mostly exist for backward com- patibility in some old programs; they will be discussed at the end. IEEE Std 1003.2 (``POSIX.2'') leaves some aspects of RE syntax and semantics open; `=' marks decisions on these aspects that may not be fully portable to other IEEE Std 1003.2 (``POSIX.2'') implementations. A (modern) RE is one= or more non-empty= branches, separated by '|'. It matches anything that matches one of the branches. A branch is one= or more pieces, concatenated. It matches a match for the first, followed by a match for the second, etc. A piece is an atom possibly followed by a single= '*', '+', '?', or bound. An atom followed by '*' matches a sequence of 0 or more matches of the atom. An atom followed by '+' matches a sequence of 1 or more matches of the atom. An atom followed by '?' matches a sequence of 0 or 1 matches of the atom. A bound is '{' followed by an unsigned decimal integer, possibly followed by ',' possibly followed by another unsigned decimal integer, always followed by '}'. The integers must lie between 0 and RE_DUP_MAX (255=) inclusive, and if there are two of them, the first may not exceed the second. An atom followed by a bound containing one integer i and no comma matches a sequence of exactly i matches of the atom. An atom followed by a bound containing one integer i and a comma matches a sequence of i or more matches of the atom. An atom followed by a bound containing two integers i and j matches a sequence of i through j (inclusive) matches of the atom. An atom is a regular expression enclosed in '()' (matching a match for the regular expression), an empty set of '()' (matching the null string)=, a bracket expression (see below), '.' (matching any single character), '^' (matching the null string at the beginning of a line), '$' (matching the null string at the end of a line), a '' followed by one of the characters '^.[$()|*+?{' (matching that character taken as an ordinary character), a '' followed by any other character= (matching that character taken as an ordinary character, as if the '' had not been present=), or a single character with no other significance (matching that character). A '{' followed by a character other than a digit is an ordinary character, not the beginning of a bound=. It is illegal to end an RE with ''. A bracket expression is a list of characters enclosed in '[]'. It normally matches any single character from the list (but see below). If the list begins with '^', it matches any single character (but see below) not from the rest of the list. If two characters in the list are separated by '-', this is shorthand for the full range of characters between those two (inclusive) in the collating sequence, e.g. '[0-9]' in ASCII matches any decimal digit. It is illegal= for two ranges to share an endpoint, e.g. 'a-c-e'. Ranges are very collating-sequence- dependent, and portable programs should avoid relying on them. To include a literal ']' in the list, make it the first character (following a possible '^'). To include a literal '-', make it the first or last character, or the second endpoint of a range. To use a literal '-' as the first endpoint of a range, enclose it in '[.' and '.]' to make it a collating element (see below). With the exception of these and some combinations using '[' (see next paragraphs), all other spe- cial characters, including '', lose their special significance within a bracket expression. Within a bracket expression, a collating element (a character, a multi-character sequence that collates as if it were a single character, or a collating-sequence name for either) enclosed in '[.' and '.]' stands for the sequence of characters of that collating element. The sequence is a single element of the bracket expression's list. A bracket expression containing a multi-character collating element can thus match more than one character, e.g. if the collating sequence includes a 'ch' collating element, then the RE '[[.ch.]]*c' matches the first five characters of 'chchcc'. Within a bracket expression, a collating element enclosed in '[=' and '=]' is an equivalence class, standing for the sequences of characters of all collating elements equivalent to that one, including itself. (If there are no other equivalent collating elements, the treatment is as if the enclosing delimiters were '[.' and '.]'.) For example, if 'x' and 'y' are the members of an equivalence class, then '[[=x=]]', '[[=y=]]', and '[xy]' are all synonymous. An equivalence class may not= be an endpoint of a range. Within a bracket expression, the name of a character class enclosed in '[:' and ':]' stands for the list of all characters belonging to that class. Standard character class names are: alnum digit punct alpha graph space blank lower upper cntrl print xdigit These stand for the character classes defined in ctype(3). A locale may provide others. A character class may not be used as an endpoint of a range. A bracketed expression like '[[:class:]]' can be used to match a single character that belongs to a character class. The reverse, matching any character that does not belong to a specific class, the negation operator of bracket expressions may be used: '[^[:class:]]'. There are two special cases= of bracket expressions: the bracket expressions '[[:<:]]' and '[[:>:]]' match the null string at the beginning and end of a word respectively. A word is defined as a sequence of word characters which is neither preceded nor followed by word charac- ters. A word character is an alnum character (as defined by ctype(3)) or an underscore. This is an extension, compatible with but not spec- ified by IEEE Std 1003.2 (``POSIX.2''), and should be used with caution in software intended to be portable to other systems. The additional word delimiters '<' and '>' are provided to ease compatibility with traditional svr4(4) systems but are not portable and should be avoided. In the event that an RE could match more than one substring of a given string, the RE matches the one starting earliest in the string. If the RE could match more than one substring starting at that point, it matches the longest. Subexpressions also match the longest possible substrings, subject to the constraint that the whole match be as long as possible, with subexpressions starting earlier in the RE taking pri- ority over ones starting later. Note that higher-level subexpressions thus take priority over their lower-level component subexpressions. Match lengths are measured in characters, not collating elements. A null string is considered longer than no match at all. For example, 'bb*' matches the three middle characters of 'abbbc', '(wee|week)(knights|nights)' matches all ten characters of 'weeknights', when '(.*).*' is matched against 'abc' the parenthesized subexpression matches all three characters, and when '(a*)*' is matched against 'bc' both the whole RE and the parenthesized subexpression match the null string. If case-independent matching is specified, the effect is much as if all case distinctions had vanished from the alphabet. When an alphabetic that exists in multiple cases appears as an ordinary character outside a bracket expression, it is effectively transformed into a bracket expression containing both cases, e.g. 'x' becomes '[xX]'. When it appears inside a bracket expression, all case counterparts of it are added to the bracket expression, so that (e.g.) '[x]' becomes '[xX]' and '[^x]' becomes '[^xX]'. No particular limit is imposed on the length of REs=. Programs intended to be portable should not employ REs longer than 256 bytes, as an implementation can refuse to accept such REs and remain POSIX-compliant. Obsolete (``basic'') regular expressions differ in several respects. '|' is an ordinary character and there is no equivalent for its func- tionality. '+' and '?' are ordinary characters, and their functionality can be expressed using bounds ('{1,}' or '{0,1}' respectively). Also note that 'x+' in modern REs is equivalent to 'xx*'. The delimiters for bounds are '{' and '}', with '{' and '}' by themselves ordi- nary characters. The parentheses for nested subexpressions are '(' and ')', with '(' and ')' by themselves ordinary characters. '^' is an ordinary character except at the beginning of the RE or= the beginning of a parenthesized subexpression, '$' is an ordinary character except at the end of the RE or= the end of a parenthesized subexpression, and '*' is an ordinary character if it appears at the beginning of the RE or the beginning of a parenthesized subexpression (after a possible leading '^'). Finally, there is one new type of atom, a back reference: '' followed by a non-zero decimal digit d matches the same sequence of characters matched by the dth parenthesized subexpression (numbering subexpressions by the positions of their opening parentheses, left to right), so that (e.g.) '([bc])1' matches 'bb' or 'cc' but not 'bc'. SEE ALSO
regex(3) Regular Expression Notation, IEEE Std, 1003.2, section 2.8. BUGS
Having two kinds of REs is a botch. The current IEEE Std 1003.2 (``POSIX.2'') spec says that ')' is an ordinary character in the absence of an unmatched '('; this was an unin- tentional result of a wording error, and change is likely. Avoid relying on it. Back references are a dreadful botch, posing major problems for efficient implementations. They are also somewhat vaguely defined (does 'a((b)*2)*d' match 'abbbd'?). Avoid using them. IEEE Std 1003.2 (``POSIX.2'') specification of case-independent matching is vague. The ``one case implies all cases'' definition given above is current consensus among implementors as to the right interpretation. The syntax for word boundaries is incredibly ugly. BSD
June 30, 2014 BSD
All times are GMT -4. The time now is 02:49 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy