Visit Our UNIX and Linux User Community


KSH Output/Strip portion of file in HP-UX


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting KSH Output/Strip portion of file in HP-UX
# 1  
Old 10-07-2009
KSH Output/Strip portion of file in HP-UX

I have a file, we'll call it file.txt. It has thousands of lines of all kinds of output at any given time (ie. foo bar foo bar)

I need to copy out just a portion of the file from Point-A to Point-B. I'd like to save off just that portion to a file called test123xyz.txt.

How do I do that?


Code:
1. foo
2. bar
3. foo
4. bar
5. <<<BEGIN test123xyz (<--Point-A)
6. foo
7. bar
8. foo
9. bar
~
~
~
~
~
~
991. foo
992. bar
993. foo
994. bar
995. >>>END test123xyz (<--Point-B)
996. foo
997. bar
998. foo

Now the good thing is, there are markers where I need to make the copy happen (ie. <<<BEGIN test123xyz)

I'm sure this is a simple grep, sed, awk or something that I can do but I've tried everything I know of with no success. Any ideas?

HP-UX 11.31, korn shell scripting.
# 2  
Old 10-07-2009
Code:
nawk ' ( $0 ~ /<<<BEGIN test123xyz/ ) || ( />>>END test123xyz/ ) { limits = limits" "NR } END { print limits } ' infile | 
xargs -i nawk -v limits={} ' BEGIN{ split( limits, n) } ( NR > n[1] ) && ( NR < n[2] ) ' infile > test123xyz.txt


Last edited by steadyonabix; 10-07-2009 at 06:18 PM.. Reason: split line for readability
# 3  
Old 10-07-2009
bash code:
  1. C="Point-A"
  2. awk '
  3.  /'$C'/ { P = 1; next }
  4.  /END/ && P { exit }
  5.  P
  6. ' file1 | tee test123xyz.txt
# 4  
Old 10-07-2009
Nice

Two questions: -

Why not: -

Code:
awk '
 /BEGIN/ { P = 1; next }
 /END/ { exit }
P
 ' infile

instead of '$C'?

and why does the P on its own line make this work?
# 5  
Old 10-07-2009
What 's wrong with a good ol'e shell script Smilie
ksh/bash/zsh/sh code:
  1. while read lineno line; do
  2.   case $line in
  3.     "<<<"*) exec 4>&1 >${line#* } ;; # save old handle, redirect output
  4.     ">>>"*) exec >&4              ;; # restore old handle
  5.          *) echo $lineno $line    ;;
  6.   esac
  7. done < infile  > outfile
It wasn't clear to me if filenumbers are part of your input files. If not, the script becomes:
ksh/bash/zsh/sh code:
  1. while read line; do
  2.   case $line in
  3.     "<<<"*) exec 4>&1 >${line#* } ;; # save old handle, redirect output
  4.     ">>>"*) exec >&4              ;; # restore old handle
  5.          *) echo $line            ;;
  6.   esac
  7. done < infile  > outfile

Last edited by Scrutinizer; 10-07-2009 at 08:11 PM..
# 6  
Old 10-07-2009
Hi.

$C was to show how shell variable could be used inside the awk (for example if it was an argument to the script:

Code:
C=${1:-Point-A}

etc.

It could have been done in other ways

Code:
/Point-A/ { ... }

Code:
awk '
...
'  C=Point-A

(and then refer to it as C instead of $C)
Code:
awk -v C=Point-A '
...
' ...

Matching "BEGIN" isn't enough anyway, as there are many BEGINs, but hopefully only one Point-A.

P makes it work because when P is true (i.e. 1) the default action (print) is executed. P is set when $C is matched.

Hoi, Scrutinizer, there is absolutely nothing wrong with the good ol' shell script! AWK is just faster if you have a fair amount of data.

Last edited by Scott; 10-07-2009 at 07:34 PM.. Reason: spelling mistakes galoore
# 7  
Old 10-08-2009
Thanks for the explanation.

It finally dawned on me last night that it must be because P evaluates true so nice and compact code, thanks.

I followed the alternatives you gave except this one: -

Code:
awk '
...
'  C=Point-A

How does setting C after the awk work?

Previous Thread | Next Thread
Test Your Knowledge in Computers #293
Difficulty: Easy
Linux is the most popular operating system among supercomputers today (2019).
True or False?

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

ksh - Read input from file and output CSV i same row

Hello I have the following output and want the output to look: FROM: GigabitEthernet0/0 is up, line protocol is up 1 input errors, 0 CRC, 0 frame, 1 overrun, 0 ignored 275 output errors, 0 collisions, 3 interface resets GigabitEthernet0/1 is up, line protocol is up 0... (4 Replies)
Discussion started by: JayJay2018
4 Replies

2. UNIX for Dummies Questions & Answers

Strip out number from wc output?

I am trying to output two command substitutions echo "$(command) $(command)" with a single echo however when using wc -l within the second substitution, that substitution, instead of appearing in order at the end of the line output, it overwrites the beginning of the output line. I've tried... (2 Replies)
Discussion started by: somegeek
2 Replies

3. Shell Programming and Scripting

ksh using input file with output going to same file name and location

I've been asked if I can write a "quick" little ksh script that will do the following: java java_class_file /dir/input_file.xml /dir/output_file.xml I'm a complete newbie with ksh so any help would be appreciated. This is on AIX and java is found in /usr/java5/jre/bin/java (4 Replies)
Discussion started by: newbie_ksh
4 Replies

4. Shell Programming and Scripting

strip csv file

Hi everyone, I hope someone can help me: i am trying to get some info from a csv file, after i awk the column i need , i made a selection and output it in a file. now i need to get a list from this file, but i stuck with some fields. basically i have a text file with next data: 3... (3 Replies)
Discussion started by: lostym
3 Replies

5. UNIX for Dummies Questions & Answers

Grab Portion of Output Text (sed, grep, awk?)

Alright, here's the deal. I'm running the following ruby script (output follows): >> /Users/name/bin/acweather.rb -z 54321 -o /Users/name/bin -c Clouds AND Sun 57/33 - Mostly sunny and cool I want to just grab the "57/33" portion, but that's it. I don't want any other portion of the line. I... (5 Replies)
Discussion started by: compulsiveguile
5 Replies

6. Shell Programming and Scripting

KSH script to run other ksh scripts and output it to a file and/or email

Hi I am new to this Scripting process and would like to know How can i write a ksh script that will call other ksh scripts and write the output to a file and/or email. For example ------- Script ABC ------- a.ksh b.ksh c.ksh I need to call all three scripts execute them and... (2 Replies)
Discussion started by: pacifican
2 Replies

7. Shell Programming and Scripting

printing portion of the output usind sed/awk

friends, i am a newbie in scripting. could someone help me in selecting only the last column of below ps command output ? mqm 14 16466 0 Sep 15 ? 0:01 /opt/mqm/bin/runmqlsr -r -m QMGR.INBOUNDSSL -t TCP -p 1415 -i 5.1.26.5 mqm 12 16700 0 Sep 15 ? 0:00... (4 Replies)
Discussion started by: unahb1
4 Replies

8. Shell Programming and Scripting

How to Strip lines off Streamed EDI Output

Attached is a streamed EDI ANSI X12 output where the segment terminator/delimiter is a tilde ~ character. Is it possible to do the following pseudo-code in a unix script (using either sed, awk and/or grep)? Open file StreamedOutput.txt Search for ISA and delete the data up to the tilde ~ char... (7 Replies)
Discussion started by: sapedi
7 Replies

9. Shell Programming and Scripting

How to strip apostrophe from a file

I am trying to remove or replace various extraneous characters from a file so that subsequent processes work correctly. The characters that is giving me trouble is the apostrophe '. The command I 'm trying is sed 's/\'//g' ${IN_WRK_DIR}/file1 > ${IN_WRK_DIR}/file2 in a Korn script on HP... (8 Replies)
Discussion started by: aquimby
8 Replies

Featured Tech Videos