Extract consecutive lines that begin with a character


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Extract consecutive lines that begin with a character
# 1  
Old 08-27-2010
Question Extract consecutive lines that begin with a character

Hello,

From a sample file below, I would like to extract only consecutive lines that begin with a '$'. How can I do this?


Code:
$ABC.1
XYGHGHGHHG
$ABC.2
RSTUVBWBB
$ABC.3
87908787798798
$QRS.5
$RST.6
679707097
$LmN.4
hgkhgh
$QRS.5
$ABC.9

Thanks in advance for your help!

~Guss

Last edited by Scott; 08-27-2010 at 03:15 PM.. Reason: Added code tags
# 2  
Old 08-27-2010
Do you mean all the lines starting with $? Then try this

Code:
sed -n "/^\$/p" file

# 3  
Old 08-27-2010
No, not quite. I want only those lines where one after another, they begin with a $ sign. For the example, it would be:

Code:
$QRS.5
$RST.6

$QRS.5
$ABC.9



~G

Last edited by Scott; 08-27-2010 at 03:15 PM.. Reason: Added code tags
# 4  
Old 08-27-2010
Code:
sed -n '/^\$/{N;/\n\$/p;}' file

# 5  
Old 08-27-2010
Quote:
Originally Posted by anbu23
Code:
sed -n '/^\$/{N;/\n\$/p;}' file

This code won't work for file containing odd number of consequtive lines beginning with "$" (3,5,7...).

@Gussifinknottle , try this:
Code:
awk '/^\$/{if(!p)x=$0;p++}!/^\$/{p=0}p==2{print x}p>1' file

# 6  
Old 08-28-2010
Code:
# cat infile
$ABC.122
XYGHGHGHHG
$ABC.222
RSTUVBWBB
$ABC.3
$ABC.4
RSTUVBWBB
$ABC.5
$ABC.6
$ABC.7
87908787798798
$QRS.8
$RST.9
$RST.10
$RST.11
679707097
$QRS.12
$QRS.13
$RST.14
$RST.15
$RST.16
hgkhgh
$QRS.17
$QRS.18
$ABC.19
$ABC.20
$ABC.21
$ABC.22
sdasdfadwe
$QRS.23
$QRS.24
$ABC.25
$ABC.26
$ABC.27
$ABC.28
$ABC.29
sdasdfadwe
 
$QRS.30
$QRS.31
$ABC.32
$ABC.33
$ABC.34
$ABC.35
$ABC.36
 
 
ddp
sdasdfadwe
 
$ABC.37
$ABC.38
$ABC.39
$QRS.40
$QRS.41
$ABC.42
$QRS.43
$QRS.44
$QRS.45
$QRS.46
$QRS.47
$QRS.48
 
$QRS.49
$ABC.50
$ABC.51

Code:
# sed -e '/^\$/N;' -e 's/.*\n[^\$]*$//;/\$/n;/\$/n;/\$/n;/\$/n;/\$/n;;s/^[^\$]*//' infile
 
 
$ABC.3
$ABC.4
 
$ABC.5
$ABC.6
$ABC.7
 
$QRS.8
$RST.9
$RST.10
$RST.11
 
$QRS.12
$QRS.13
$RST.14
$RST.15
 
$RST.16
$QRS.17
$QRS.18
$ABC.19
$ABC.20
$ABC.21
 
$ABC.22
$QRS.23
$QRS.24
$ABC.25
$ABC.26
$ABC.27
$ABC.28
$ABC.29
 
 
$QRS.30
$QRS.31
$ABC.32
$ABC.33
$ABC.34
$ABC.35
$ABC.36
 
 
 
 
$ABC.37
$ABC.38
$ABC.39
$QRS.40
$QRS.41
$ABC.42
$QRS.43
$QRS.44
$QRS.45
$QRS.46
$QRS.47
$QRS.48
 
$QRS.49
$ABC.50
$ABC.51

# 7  
Old 08-29-2010
Hi.

Using awk ability to read mulitple lines as one record:
Code:
#!/usr/bin/env bash

# @(#) s1	Demonstrate treating lines as fields in awk for counting.

# Section 1, setup, pre-solution.
# Infrastructure details, environment, commands for forum posts. 
# Uncomment export command to run script as external user.
# export PATH="/usr/local/bin:/usr/bin:/bin"
set +o nounset
pe() { for i;do printf "%s" "$i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }
LC_ALL=C ; LANG=C ; export LC_ALL LANG
pe ; pe "Environment: LC_ALL = $LC_ALL, LANG = $LANG"
pe "(Versions displayed with local utility \"version\")"
c=$( ps | grep $$ | awk '{print $NF}' )
version >/dev/null 2>&1 && s=$(_eat $0 $1) || s=""
[ "$c" = "$s" ] && p="$s" || p="$c"
version >/dev/null 2>&1 && version "=o" $p printf specimen sed awk
set -o nounset
pe

FILE=${1-data1}

# Display sample of data file, with head & tail as a last resort.
pe " || start [ first:middle:last ]"
specimen 7 $FILE \
|| { pe "(head/tail)"; head -n 5 $FILE; pe " ||"; tail -n 5 $FILE; }
pe " || end"

# Section 2, solution.
pl " Results:"
sed 's/^[^\$].*//' $FILE |
awk '
BEGIN	{ RS = "" ; FS = "\n" ; ORS = "\n\n" }
# Uncomment following lines to see number of fields in multi-line group
# NF > 1	{ print " Input line is " NR " number of fields is " NF }
NF > 1	{ print $0 }
' |
specimen 

# Section 3, post-solution, check results, clean-up, etc.

exit 0

producing:
Code:
% ./s1

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64
Distribution        : Debian GNU/Linux 5.0 
GNU bash 3.2.39
printf - is a shell builtin [bash]
specimen (local) 1.17
GNU sed version 4.1.5
GNU Awk 3.1.5

 || start [ first:middle:last ]
Whole: 7:0:7 of 13 lines in file "data1"
$ABC.1
XYGHGHGHHG
$ABC.2
RSTUVBWBB
$ABC.3
87908787798798
$QRS.5
$RST.6
679707097
$LmN.4
hgkhgh
$QRS.5
$ABC.9
 || end

-----
 Results:
Whole: 5:0:5 of 6 lines in file "-"
$QRS.5
$RST.6

$QRS.5
$ABC.9

I suspect that some awk folks could produce a script that could eliminate the sed command that replaces the separators.

Best wishes ... cheers, drl

Last edited by drl; 08-30-2010 at 07:50 AM.. Reason: ( Edit 1: for clarity, replaced "OFS" with "ORS", removed "\n" from print )
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Delete Directories and its files that begin with the same character string

I wrote a shell script program that supposed to delete any directories and its files that begin with the same character string . The program is designed to delete directories and its files that are one day old or less and only the directories that begin with the same character string. The problem... (1 Reply)
Discussion started by: dellanicholson
1 Replies

2. Shell Programming and Scripting

Grep three consecutive lines if each lines contains certain string

say we have : 2914 | REQUEST | whatever 2914 | RESPONSE | whatever 2914 | SUCCESS | whatever 2985 | RESPONSE | whatever 2986 | REQUEST | whatever 2990 | REQUEST | whatever 2985 | RESPONSE | whatever 2996 | REQUEST | whatever 2010 | SUCCESS | whatever 2013 | REQUEST | whatever 2013 |... (7 Replies)
Discussion started by: Saumitra Pandey
7 Replies

3. Shell Programming and Scripting

sed - remove begin of line up to the third and including occurence of character

hello. How to remove all characters in a line from first character ( a $ ) until and including the third occurrence of that character ( $ ). Any help is welcome. (10 Replies)
Discussion started by: jcdole
10 Replies

4. Shell Programming and Scripting

Inserting new line if two sequential lines begin with the same string

Hi I have a file like this: Peter North Mary Peter North Peter Borough Mary I need there to put 'X' (or anything) on a new line between the two lines where 'Peter' begins the line. There will be many matches to this string, but they will always begin with 'Peter'. Ie, the resulting... (2 Replies)
Discussion started by: majormajormajor
2 Replies

5. Shell Programming and Scripting

Grep couple of consecutive lines if each lines contains certain string

Hello, I want to extract from a file like : 20120530025502914 | REQUEST | whatever 20120530025502968 | RESPONSE | whatever 20120530025502985 | RESPONSE | whatever 20120530025502996 | REQUEST | whatever 20120530025503013 | REQUEST | whatever 20120530025503045 | RESPONSE | whatever I want... (14 Replies)
Discussion started by: black_fender
14 Replies

6. Shell Programming and Scripting

Merge two non-consecutive lines

Hello - First post here. I need help combining two lines that are non-consecutive in a file. Using sed, awk or perl preferably. So the file looks as follows. Please note, the "Line#:" is there only for reference. The lines can only be distinguished by whether they have "start" or "done" in... (2 Replies)
Discussion started by: munkee
2 Replies

7. Shell Programming and Scripting

need to create lines from ones that begin with the field separator

Hello, I need to modify an awk script to recognize the last field $NF when the line is split over more than 1 line. In my input file the field separator is the exclamation mark ! so FS="!" So here is my input file infile.txt, it has 2 records, the field separator is in bold: INPUT ... (6 Replies)
Discussion started by: script_op2a
6 Replies

8. Shell Programming and Scripting

Using AWK BEGIN to extract file header info into variables

Hi Folks, I've searched for this for quite a while, but can't find any solution - hope someone can help. I have various files with standard headers. eg. <HEADER> IP: 1.2.3.4 Username: Joe Time: 12:00:00 Date: 23/05/2010 </HEADER> This is a test and this part can be any size... (6 Replies)
Discussion started by: damoske
6 Replies

9. Shell Programming and Scripting

How to skip lines which don't begin with a number

Hi, I have a file: file.txt 1 word 2 word word word 3 word 4 word and I would like to create a set: set number = `cut -d" " -f1 ${1}` #${1} is the text file but it should only contain the lines which begin with numbers, and another set which contains the lines which begin with... (10 Replies)
Discussion started by: shira
10 Replies

10. Shell Programming and Scripting

Appending Consecutive lines

Hi, I have a file containing a single field on every row. What I need is to append one on to the end of another, e.g. The input file looks like this: nnnnn mmmmmm nnnnn mmmmmm I need it to look like this: nnnnn mmmmmm nnnnn mmmmmm Any ideas would be much appreciated,... (8 Replies)
Discussion started by: pondlife
8 Replies
Login or Register to Ask a Question