![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here. |
|
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Read a file line by line | VENC22 | UNIX for Dummies Questions & Answers | 4 | 4 Weeks Ago 08:09 AM |
| Read value from particular position in file. | krishnarao | Shell Programming and Scripting | 2 | 05-15-2008 03:49 AM |
| read space filled file and replace text at specific position | COD | Shell Programming and Scripting | 6 | 04-21-2008 02:40 AM |
| read or search the item in a file sequentially by position using unix shell script? | lok | UNIX for Dummies Questions & Answers | 6 | 07-12-2006 03:53 AM |
| How to read from a file line by line and do stuff | spaceship | Shell Programming and Scripting | 4 | 03-17-2005 06:47 PM |
|
|
Submit Tools | LinkBack | Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
read file until certain line position
let's say I have this file format
Quote:
Quote:
Last edited by finalight; 05-21-2008 at 12:22 AM. |
| Forum Sponsor | ||
|
|
|
#2
|
||||
|
||||
|
Quote:
If the input is all single column without separators; start=grep -ni '^test' | awk '{FS=":"};{print $1}' end=grep -ni '^*' | awk '{FS=":"};{print $1}' start will then equal the line before you want to begin and end the line after Using expr or any of the other tools you can then add one to start and subract one from end and have the range you need to work with. If you're using perl etc then in a while loop look for test and then process until the variable equals * |
|
#3
|
|||
|
|||
|
If you grab the values between the separators into a variable then echoing that variable without quoting it will "magically" condense the whitespace into one space per run of whitespace.
That doesn't work too well if the input is supposed to contain the asterisk, though, because that will be expanded as a wildcard. Instead, you can use tr to change newlines to spaces: Code:
variable1=`sed -n '/^test$/,/^\*\*$/p file1 | tr '\012' ' '` variable2=`sed -n '/^test$/,/^\*$/p' file2 | tr '\012' ' '` If your tr doesn't understand '\012' to mean newline, see its manual page, or search these forums for a solution; it has been posted multiple times, but there are too many different variations to summarize here. |
|
#4
|
|||
|
|||
|
Here is one way of doing what you want to do - but requires ksh93
Code:
#!/bin/ksh93 # # showme # TMP=file.$$ cat <<'EOT' >$TMP first test dfgsdgs sdgsg 3 4 sd 6 sdgsdg 8 9 ** last EOT # read required lines into var var=$( exec 3< $TMP 3<#'test' 3<# ((CUR + 5)) 3<##'\*\**' exec 3<&- ) rm $TMP # remove newlines tmp=$(print $var) print "var: $tmp" exit 0 Code:
$ ./showme dfgsdgs sdgsg 3 4 sd 6 sdgsdg 8 9 $ Last edited by fpmurphy; 05-20-2008 at 05:25 PM. |
|
#5
|
|||
|
|||
|
sed -n '/test/,/*/p' file
|
|
#6
|
|||
|
|||
|
Actually plain * is a syntax error, you need to backslash it.
|
|||
| Google The UNIX and Linux Forums |