Sponsored Content
Top Forums Shell Programming and Scripting Extracting fixed length number from a text file Post 302994211 by dsid on Monday 20th of March 2017 12:33:02 PM
Old 03-20-2017
@vgersh. awk -F ' ' is for defining delimiters as far as I know, what does your command mean
Code:
awk -F'[][]'

. The next part is the start of the string I got that and I think $NF would be number of fields, but why are you subtracting 1 from it and then printing that out.

@Scrutinizer: true my command did print out numbers more than 23 in length.


Code:
grep  -Eo '\<[0-9]{23}\>' file

the above command should work perfect as square brackets wont be in delimiters always so the awk command wont work in all occasions.

I am kinda confused as to why we are using single quotes in a grep expression. Cos I was reading the other day that single quotes remove any meaning from the special characters. Shouldn't we use double quotes?

Also the fact the you have used <,>. Are they working as a block to extract only 23 digits numbers/characters?

---------- Post updated at 04:33 PM ---------- Previous update was at 04:25 PM ----------

@andy391791

I tweaked your command a bit cos the square brackets may or may not be present in future texts for finding the 23 digit number. The tweak seems to be working fine

Code:
$ echo 'Original presentment record for ARN  24013935350549886999873 not found'|sed -n 's/.*\([0-9]\{23\}\).*/\1/p'
24013935350549886999873
[dsiddiqui@lxserv01 scripts]$ echo 'Original presentment record for ARN  [24013935350549886999873] not found'|sed -n 's/.*\([0-9]\{23\}\).*/\1/p'
24013935350549886999873

but when I increase the length of the 23 digit number and run the command, it just extracts 23 numbered digits from the complete numeric strings and pastes it, which is not the case that I want. I just want to print 23 digits only if present
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

creating a fixed length output from a variable length input

Is there a command that sets a variable length? I have a input of a variable length field but my output for that field needs to be set to 32 char. Is there such a command? I am on a sun box running ksh Thanks (2 Replies)
Discussion started by: r1500
2 Replies

2. Shell Programming and Scripting

convert XML file into Text file(fixed length)

If someone out there could help me out with this problem. I would really appreciate it. I am trying to convert xml into text file(fixed length) using Unix Borne shell scripts. My xml file: <root> <header_rec recordtype="00"> <record_id>00</record_id> ... (0 Replies)
Discussion started by: ram2s2001
0 Replies

3. UNIX for Dummies Questions & Answers

What the command to find out the record length of a fixed length file?

I want to find out the record length of a fixed length file? I forgot the command. Any body know? (9 Replies)
Discussion started by: tranq01
9 Replies

4. UNIX for Dummies Questions & Answers

Convert a tab delimited/variable length file to fixed length file

Hi, all. I need to convert a file tab delimited/variable length file in AIX to a fixed lenght file delimited by spaces. This is the input file: 10200002<tab>US$ COM<tab>16/12/2008<tab>2,3775<tab>2,3783 19300978<tab>EURO<tab>16/12/2008<tab>3,28523<tab>3,28657 And this is the expected... (2 Replies)
Discussion started by: Everton_Silveir
2 Replies

5. Shell Programming and Scripting

fixed length text file padding issues in AIX

Hi, I have a fixed length text file that needs to be cut into individual files in aix and facing padding issues. If I have multiple blank spaces in the file it is just making it one while cutting the files.. Eg:- $ - blank space filename:file.txt ... (2 Replies)
Discussion started by: techmoris
2 Replies

6. Shell Programming and Scripting

changing a variable length text to a fixed length

Hi, Can anyone help with a effective solution ? I need to change a variable length text field (between 1 - 18 characters) to a fixed length text of 18 characters with the unused portion, at the end, filled with spaces. The text field is actually field 10 of a .csv file however I could cut... (7 Replies)
Discussion started by: dc18
7 Replies

7. Shell Programming and Scripting

Help with extracting words from fixed length files

I am very new to scripting and need to write a script that will extract the account number from a line that begins with HDR. For example, the file is as follows HDR2010072600300405505100726 00300405505 LBJ FREEWAY DALLAS TELEGRAPH ... (9 Replies)
Discussion started by: bds052189
9 Replies

8. Shell Programming and Scripting

Insert a variable to a text file after fixed number of lines

Hi, I am new to unix. I need to insert a variable which contains some lines of text into a text file after fixed number of lines.. Please help me on this.. Thanks in Advance, Amrutha (3 Replies)
Discussion started by: amr89
3 Replies

9. UNIX for Dummies Questions & Answers

Fixed length file extracting values in columns

How do I extract values in a few columns in a row of a fixed length file? If there are 8 columns and I need to extract values of 2nd,4th and 6 th columns, how do i do that? I used cut command, this I used only for one column. How do I do it more than one column? The below command will give... (1 Reply)
Discussion started by: princetd001
1 Replies

10. UNIX for Dummies Questions & Answers

Length of a fixed width file

I have a fixed width file of length 53. when is try to get the lengh of the record of that file i get 2 different answers. awk '{print length;exit}' <File_name> The above code gives me length 50. wc -L <File_name> The above code gives me length 53. Please clarify on... (2 Replies)
Discussion started by: Amrutha24
2 Replies
grep(1) 						      General Commands Manual							   grep(1)

Name
       grep, egrep, fgrep - search file for regular expression

Syntax
       grep [option...] expression [file...]

       egrep [option...] [expression] [file...]

       fgrep [option...] [strings] [file]

Description
       Commands  of  the family search the input files (standard input default) for lines matching a pattern.  Normally, each line found is copied
       to the standard output.

       The command patterns are limited regular expressions in the style of which uses a compact nondeterministic algorithm.  The command patterns
       are  full  regular  expressions.  The command uses a fast deterministic algorithm that sometimes needs exponential space.  The command pat-
       terns are fixed strings.  The command is fast and compact.

       In all cases the file name is shown if there is more than one input file.  Take care when using the characters $ * [ ^ | ( ) and   in  the
       expression because they are also meaningful to the Shell.  It is safest to enclose the entire expression argument in single quotes ' '.

       The command searches for lines that contain one of the (new line-separated) strings.

       The command accepts extended regular expressions.  In the following description `character' excludes new line:

	      A  followed by a single character other than new line matches that character.

	      The character ^ matches the beginning of a line.

	      The character $ matches the end of a line.

	      A .  (dot) matches any character.

	      A single character not otherwise endowed with special meaning matches that character.

	      A  string  enclosed in brackets [] matches any single character from the string.	Ranges of ASCII character codes may be abbreviated
	      as in `a-z0-9'.  A ] may occur only as the first character of the string.  A literal - must be placed where it can't be mistaken	as
	      a range indicator.

	      A  regular  expression  followed	by  an	* (asterisk) matches a sequence of 0 or more matches of the regular expression.  A regular
	      expression followed by a + (plus) matches a sequence of 1 or more matches of the regular expression.  A regular expression  followed
	      by a ? (question mark) matches a sequence of 0 or 1 matches of the regular expression.

	      Two regular expressions concatenated match a match of the first followed by a match of the second.

	      Two regular expressions separated by | or new line match either a match for the first or a match for the second.

	      A regular expression enclosed in parentheses matches a match for the regular expression.

       The  order  of  precedence  of  operators at the same parenthesis level is the following:  [], then *+?, then concatenation, then | and new
       line.

Options
       -b	   Precedes each output line with its block number.  This is sometimes useful in locating disk block numbers by context.

       -c	   Produces count of matching lines only.

       -e expression
		   Uses next argument as expression that begins with a minus (-).

       -f file	   Takes regular expression (egrep) or string list (fgrep) from file.

       -i	   Considers upper and lowercase letter identical in making comparisons and only).

       -l	   Lists files with matching lines only once, separated by a new line.

       -n	   Precedes each matching line with its line number.

       -s	   Silent mode and nothing is printed (except error messages).	This is useful for checking the error status (see DIAGNOSTICS).

       -v	   Displays all lines that do not match specified expression.

       -w	   Searches for an expression as for a word (as if surrounded by `<' and `>').  For further information, see only.

       -x	   Prints exact lines matched in their entirety only).

Restrictions
       Lines are limited to 256 characters; longer lines are truncated.

Diagnostics
       Exit status is 0 if any matches are found, 1 if none, 2 for syntax errors or inaccessible files.

See Also
       ex(1), sed(1), sh(1)

																	   grep(1)
All times are GMT -4. The time now is 02:46 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy