Sponsored Content
Top Forums Shell Programming and Scripting Extract X words from end of line, minus last keynumber X Post 302466213 by fubaya on Tuesday 26th of October 2010 01:14:09 AM
Old 10-26-2010
Extract X words from end of line, minus last keynumber X

The file contains one line of text followed by a number. I want to take the number X at the end, take it out and display the last X words. X is the key telling me how many words from the end that I want and X will always be less than the number of words, so no problem there.

Example input and desired output:

input: this is the file with the text 1
output: text

input: this is the file with the text 2
output: the text

input: this is the file with the text 3
output: with the text

input: this is the file with the text 4
output: file with the text

I can do this with shell and sed, but it's not elegant and there has to be a simpler solution. awk print column would be simple, except I need to go from the right side of the line instead of the left.

My quick solution, without getting into arithmetic, is to use tr to change spaces to newlines, tail -1 to extract the number, sed to get rid of the last line, then tr to change newlines back to spaces. It works fine, but now I think this will become a permanent part of the script and it's just too ugly and I'm just too braindead to figure out something different.
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

extract a particular start and end pattern from a line

hi In the foll example the whole text in a single line.... i want to extract text from IPTel to RTCPBase.h. want to use this acrooss the whole file Updated: IPTel\platform\core\include\RTCPBase.h \main\MWS2051_Sablime_Int\1... (7 Replies)
Discussion started by: manish205
7 Replies

2. Shell Programming and Scripting

How to append words at the end of first line

Hi Unix gurus This is my first post here. I have a file which looks like this: string1,string2 ,string3 ,string4 ... ... I need to append all words below first line to the first line, ie string1,string2,string3,string4,... Bear in mind that it is not known how many lines follow... (11 Replies)
Discussion started by: lmatlebyane
11 Replies

3. UNIX for Dummies Questions & Answers

Using sed to extract a substring at end of line

This is the line that I am using: sed 's/^*\({3}*$\)/\1 /' <test.txt >results.txt and suppose that test.txt contains the following lines: http://www.example.com/200904/AUS.txt http://www.example.com/200903/_RUS.txt http://www.example.com/200902/.FRA.txt What I expected to see in results.txt... (6 Replies)
Discussion started by: figaro
6 Replies

4. Shell Programming and Scripting

sed: Find start of pattern and extract text to end of line, including the pattern

This is my first post, please be nice. I have tried to google and read different tutorials. The task at hand is: Input file input.txt (example) abc123defhij-E-1234jslo 456ujs-W-abXjklp From this file the task is to grep the -E- and -W- strings that are unique and write a new file... (5 Replies)
Discussion started by: TestTomas
5 Replies

5. Shell Programming and Scripting

Copying x words from end of line to specific location in same line

Hello all i know it is pretty hard one but you will manage it all after noticing and calculating i find a rhythm for the file i want to edit to copy the last 12 characters in line but the problem is to add after first 25 characters in same line in other way too copy the last 12 characters... (10 Replies)
Discussion started by: princesasa
10 Replies

6. UNIX for Dummies Questions & Answers

extract text between two words on a single line

Hi Guys, Can someone help me with a way to extract text between two words on a single line. For example if the file has below content I want to extract all text between b and f inclusive of b and f. Aparently sed does this but does it line by line and I guess it cannot read word by word. ... (11 Replies)
Discussion started by: krishnaux
11 Replies

7. Shell Programming and Scripting

How to extract text from STRING to end of line?

Hi I have a very large data file with several hundred columns and millions of lines. The important data is in the last set of columns with variable numbers of tab delimited fields in front of it on each line. Im currently trying sed to get the data out - I want anything beetween :RES and... (4 Replies)
Discussion started by: Manchesterpaul
4 Replies

8. Shell Programming and Scripting

Print one sentence 40 to 50 words end with period in a file

Hi All, Is there another way to achieve this? how get short phrase in a sentence with character count of 100 to 155 words end with period but don't end something like 50,000. . Here's my current script but the output is not good. This will use for my snippets or preview. grep... (6 Replies)
Discussion started by: lxdorney
6 Replies

9. Shell Programming and Scripting

Replace particular words in file based on if finds another words in that line

Hi All, I need one help to replace particular words in file based on if finds another words in that file . i.e. my self is peter@king. i am staying at north sydney. we all are peter@king. How to replace peter to sham if it finds @king in any line of that file. Please help me... (8 Replies)
Discussion started by: Rajib Podder
8 Replies

10. Shell Programming and Scripting

Perl to extract values and print at end of each line

In the below perl I am trying to extract and print the values AF1=, the GT value, and F or QUAL diveded by 33 (rounded to the nearest whole #). The GT value is at the end after the GT:PL so all the possibilities are read into a hash h, then depending on the value that is in the line the... (1 Reply)
Discussion started by: cmccabe
1 Replies
OPENPAM_READLINEV(3)					   BSD Library Functions Manual 				      OPENPAM_READLINEV(3)

NAME
openpam_readlinev -- read a line from a file and split it into words LIBRARY
Pluggable Authentication Module Library (libpam, -lpam) SYNOPSIS
#include <sys/types.h> #include <stdio.h> #include <security/pam_appl.h> #include <security/openpam.h> char ** openpam_readlinev(FILE *f, int *lineno, int *lenp); DESCRIPTION
The openpam_readlinev() function reads a line from a file, splits it into words according to the rules described in the openpam_readword(3) manual page, and returns a list of those words. If lineno is not NULL, the integer variable it points to is incremented every time a newline character is read. This includes quoted or escaped newline characters and the newline character at the end of the line. If lenp is not NULL, the number of words on the line is stored in the variable to which it points. RETURN VALUES
If successful, the openpam_readlinev() function returns a pointer to a dynamically allocated array of pointers to individual dynamically allocated NUL-terminated strings, each containing a single word, in the order in which they were encountered on the line. The array is ter- minated by a NULL pointer. The caller is responsible for freeing both the array and the individual strings by passing each of them to free(3). If the end of the line was reached before any words were read, openpam_readlinev() returns a pointer to a dynamically allocated array con- taining a single NULL pointer. The openpam_readlinev() function can fail and return NULL for one of four reasons: o The end of the file was reached before any words were read; errno is zero, ferror(3) returns zero, and feof(3) returns a non-zero value. o The end of the file was reached while a quote or backslash escape was in effect; errno is set to EINVAL, ferror(3) returns zero, and feof(3) returns a non-zero value. o An error occurred while reading from the file; errno is non-zero, ferror(3) returns a non-zero value and feof(3) returns zero. o A malloc(3) or realloc(3) call failed; errno is set to ENOMEM, ferror(3) returns a non-zero value, and feof(3) may or may not return a non-zero value. SEE ALSO
openpam_readline(3), openpam_readword(3), pam(3) STANDARDS
The openpam_readlinev() function is an OpenPAM extension. AUTHORS
The openpam_readlinev() function and this manual page were developed by Dag-Erling Smorgrav <des@des.no>. BSD
September 12, 2014 BSD
All times are GMT -4. The time now is 02:54 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy