![]() |
|
|
|
|
|||||||
| 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 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 |
| Read a file line by line | VENC22 | UNIX for Dummies Questions & Answers | 2 | 05-12-2005 10:13 AM |
| How to read from a file line by line and do stuff | spaceship | Shell Programming and Scripting | 4 | 03-17-2005 05:47 PM |
|
|
Submit Tools | LinkBack | Thread Tools | Display Modes |
|
|||
|
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 | ||
|
|
|
|||
|
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. |
|
|||
|
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. |