4

From the bash command shell, parse the numbers of a line

 2 years ago
source link: https://www.codesd.com/item/from-the-bash-command-shell-parse-the-numbers-of-a-line.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

From the bash command shell, parse the numbers of a line

advertisements

I have a log line that I am pulling from bash like:

There are 5 apples and 7 oranges

And I want to get the 5 and 7 into bash variables and am having problems doing so.

awk '{ print $3, $6 }' won't work due to the fact that their position might change, if there are bananas. Bonus points if the digits can be associated with their fruits.

Thanks in advance


and to get the fruits:

echo 'There are 5 apples and 7 oranges' | grep -o -E '[[:digit:]]+ [[:alpha:]]+'

Tags bash

Related Articles

Delete numbers from the beginning of a line in a string with PHP

I have a bunch of strings in php that all look like this: 10 NE HARRISBURG 4 E HASWELL 2 SE OAKLEY 6 SE REDBIRD PROVO 6 W EADS 21 N HARRISON What I am needing to do is remove the numbers and the letters from before the city names. The problem I am ha

How to remove the character from the numbers

This question already has an answer here: How to strip all non-alphabetic characters from string in SQL Server? 19 answers Using SQL 2008 ID: C/213434343434334, CA/2566121212122 From the above id, i want to take only numbers. How to acheive this Requ

How to classify (prioritize) values ​​from the numbered check box form in PHP

I am using the code below to ask users to rank which programming language they are more comfortable with. The users need to rank from 1-3 (1 being the one they are most comfortable with) <form id="form1" name="form1" method="po

Delete the mail string from the beginning of each line in a file

I have a file with the following format [email protected] some string with no set width [email protected] another string [email protected] yet another string!! [email protected] shortstring What I want to do is run a bash command that will get

Find and replace inside a text file from a Bash command

What's the simplest way to do a find and replace for a given input string, say abc, and replace with another string, say XYZ in file /tmp/file.txt? I am writting an app and using IronPython to execute commands through SSH - but I don't know Unix that

Deleting multiple spaces from the beginning of each line in a file in tcl

I want to remove the spaces from the starting of each and line of a file so used this set fp [open "test_one.tcl" r] set file_data [read $fp] set lines [split $file_data "\n"] regsub -all {^\s+} $lines "" after running this a

Display integers from the file on a line

I have the very simple task of creating a text file with 8 random integers from 1-100, reading the file, displaying the numbers on the same line, calculating the even integers and the odd integers, and then displaying them. The problem I am having is

The program copies specific lines from the HTML file; Some lines break it without visible motive?

I have written a program in Python to take the text from specific elements in an HTML file ('input') and write it all into another document ('output'). The program works by searching for the particular HTML tag that precedes all elements of the desir

Vim: visual mode from the end of a line

I am new to using Vim. In other text editors, I can go to the end of a line and start highlighting characters on that line including the last character on the line. In vim, is there a way to select characters from the end of the line in visual mode?

Remove the empty space from the end of a line in a richtext field

How can I remove empty space from end of line in a richtextbox Original string My code: string[] lines2 = comparedResultRTB1.Lines; lines2.ToString().Replace("; );",");"); Could use Regex to do the replacement. Not sure from the questi

How to rearrange the numbers of different lines of a text file in python?

So I have a text file consisting of one column, each column consist two numbers 190..255 337..2799 2801..3733 3734..5020 5234..5530 5683..6459 8238..9191 9306..9893 I would like to discard the very 1st and the very last number, in this case, 190 and

java- How do I count spaces between paragraphs and subtract them from the total number of lines?

If I read a file the have empty spaces between paragraphs, how do I count the empty spaces and subtract them from the total line count? public int countLinesInFile(File f){ int lines = 0; try { BufferedReader reader = new BufferedReader(new FileReade

In Go, scan the numbers of a line using recursion

I want to scan a line of integers from stdin into a slice of integers. Each integer is separated by whitespace. Ther would be as many as N integers of user input. I'm trying not to use a for loop. For example, 1 15 16 17 So far, this is my function t

Exclude records from the first and last lines of a loop

Below is my export to csv from listview using vb.net Function ExportListview2CSV(ByVal lstview As ListView) As Boolean Dim saveFileDialog1 As New SaveFileDialog() Dim csvFileContents As New System.Text.StringBuilder Dim CurrLine As String = String.Em

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK