Sponsored Content
Top Forums Shell Programming and Scripting numbering each line in a text file Post 302297172 by Ikon on Thursday 12th of March 2009 05:46:02 PM
Old 03-12-2009
Code:
# number each line of a file (simple left alignment). Using a tab (see note on '\t' at end of file) instead of space will preserve margins.
sed = filename | sed 'N;s/\n/\t/'
# number each line of a file (number on left, right-aligned)
sed = filename | sed 'N; s/^/     /; s/ *\(.\{6,\}\)\n/\1  /'
# number each line of file, but only print numbers if line is not blank
sed '/./=' filename | sed '/./N; s/\n/ /'

 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

AWK Multi-Line Records Numbering Problem

I have a set of files of multi-line records with the records separated by a blank line. I needed to add a record number to the front of each line followed by a colon and did the following: awk 'BEGIN {FS = "\n"; RS = ""}{for (i=1; i<=NF; i++)print NR,":",$i}' ~/Desktop/data98-1-25.txt >... (3 Replies)
Discussion started by: RacerX
3 Replies

2. Shell Programming and Scripting

Adding specific text and spaces to each line in a text file

Hi, I wanted to add specific text to each row in a text file containing three rows. Example: 0 8 7 6 5 5 7 8 9 0 7 9 7 8 9 0 1 2 And I want to add a 21 at the beginning of the first row, and blank spaces at the beginning of the second two rows. To get this: 21 0 8 7 6 5 5 7 8... (4 Replies)
Discussion started by: hertingm
4 Replies

3. Shell Programming and Scripting

How to insert some constant text at beginig of each line within a text file.

Dear Folks :), I am new to UNIX scripting and I do not know how can I insert some text in the first column of a UNIX text file at command promtp. I can do this in vi editor by using this command :g/^/s//BBB_ e,g I have a file named as Test.dat and it containins below text: michal... (4 Replies)
Discussion started by: Muhammad Afzal
4 Replies

4. Shell Programming and Scripting

Numbering a Text File

Running into a little problem with blank lines. My file is of this format: To number each line of the file i would use: n=1 echo "$FILE" | while read line do echo "$n) $line" n=`expr $n + 1` But really, i dont want to number the blank lines. What i've tried is to use sed... (13 Replies)
Discussion started by: omgsomuchppl
13 Replies

5. Shell Programming and Scripting

Search text from a file and print text and one previous line too

Hi, Please let me know how to find text and print text and its previous line. Please don't get irritated few days back I asked text and next line. I am using HP-UX 11.11 Thanks for your help. (6 Replies)
Discussion started by: kamranjalal
6 Replies

6. Shell Programming and Scripting

sed or awk help - line numbering w/ different start value

I'm pretty new to sed and awk, and I can't quite figure this one out. I've been trying with sed, as I'm more comfortable with it for the time being, but any tool that fits the bill will be fine. I have a few files, whose contents appear more or less like so: 1|True|12094856|12094856|Test|... (7 Replies)
Discussion started by: camwheel
7 Replies

7. Shell Programming and Scripting

start line numbering with arbitrary number?

Hi, I want to do the following: Extract some lines from different files and copy them into one file, with the first column being the line number. I do this with cat file1 file2 file3 |grep 'xxx' |nl > output.file Works fine. But if I want to add data of interest from a fourth file to the... (2 Replies)
Discussion started by: paracetamol
2 Replies

8. UNIX for Dummies Questions & Answers

Extracting lines from a text file based on another text file with line numbers

Hi, I am trying to extract lines from a text file given a text file containing line numbers to be extracted from the first file. How do I go about doing this? Thanks! (1 Reply)
Discussion started by: evelibertine
1 Replies

9. Shell Programming and Scripting

Reading text file, comparing a value in a line, and placing only part of the line in a variable?

I need some help. I would like to read in a text file. Take a variable such as ROW-D-01, compare it to what's in one line in the text file such as PROD/VM/ROW-D-01 and only input PROD/VM into a variable without the /ROW-D-01. Is this possible? any help is appreciated. (2 Replies)
Discussion started by: xChristopher
2 Replies

10. Shell Programming and Scripting

awk use sequential line numbering in output

The awk below produces an output with the original header and only the matching lines (which is good), but the output where the original line numbering in the match found on is used. I can not figure out how to sequentially number the output instead of using the original. I did try to add... (2 Replies)
Discussion started by: cmccabe
2 Replies
TAP::Parser::Result::Test(3)				User Contributed Perl Documentation			      TAP::Parser::Result::Test(3)

NAME
TAP::Parser::Result::Test - Test result token. VERSION
Version 3.28 DESCRIPTION
This is a subclass of TAP::Parser::Result. A token of this class will be returned if a test line is encountered. 1..1 ok 1 - woo hooo! OVERRIDDEN METHODS
This class is the workhorse of the TAP::Parser system. Most TAP lines will be test lines and if "$result->is_test", then you have a bunch of methods at your disposal. Instance Methods "ok" my $ok = $result->ok; Returns the literal text of the "ok" or "not ok" status. "number" my $test_number = $result->number; Returns the number of the test, even if the original TAP output did not supply that number. "description" my $description = $result->description; Returns the description of the test, if any. This is the portion after the test number but before the directive. "directive" my $directive = $result->directive; Returns either "TODO" or "SKIP" if either directive was present for a test line. "explanation" my $explanation = $result->explanation; If a test had either a "TODO" or "SKIP" directive, this method will return the accompanying explanation, if present. not ok 17 - 'Pigs can fly' # TODO not enough acid For the above line, the explanation is not enough acid. "is_ok" if ( $result->is_ok ) { ... } Returns a boolean value indicating whether or not the test passed. Remember that for TODO tests, the test always passes. If the test is unplanned, this method will always return false. See "is_unplanned". "is_actual_ok" if ( $result->is_actual_ok ) { ... } Returns a boolean value indicating whether or not the test passed, regardless of its TODO status. "actual_passed" Deprecated. Please use "is_actual_ok" instead. "todo_passed" if ( $test->todo_passed ) { # test unexpectedly succeeded } If this is a TODO test and an 'ok' line, this method returns true. Otherwise, it will always return false (regardless of passing status on non-todo tests). This is used to track which tests unexpectedly succeeded. "todo_failed" # deprecated in favor of 'todo_passed'. This method was horribly misnamed. This was a badly misnamed method. It indicates which TODO tests unexpectedly succeeded. Will now issue a warning and call "todo_passed". "has_skip" if ( $result->has_skip ) { ... } Returns a boolean value indicating whether or not this test has a SKIP directive. "has_todo" if ( $result->has_todo ) { ... } Returns a boolean value indicating whether or not this test has a TODO directive. "as_string" print $result->as_string; This method prints the test as a string. It will probably be similar, but not necessarily identical, to the original test line. Directives are capitalized, some whitespace may be trimmed and a test number will be added if it was not present in the original line. If you need the original text of the test line, use the "raw" method. "is_unplanned" if ( $test->is_unplanned ) { ... } $test->is_unplanned(1); If a test number is greater than the number of planned tests, this method will return true. Unplanned tests will always return false for "is_ok", regardless of whether or not the test "has_todo". Note that if tests have a trailing plan, it is not possible to set this property for unplanned tests as we do not know it's unplanned until the plan is reached: print <<'END'; ok 1 ok 2 1..1 END perl v5.16.3 2013-05-02 TAP::Parser::Result::Test(3)
All times are GMT -4. The time now is 11:10 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy