shell , Missing one condition to display a string


Missing one condition to display a string

Question:

Tag: shell

I am working on a small script that simply takes a parameter and read the output of ls -l and displays the user and name of the file that start with that parameter.

For exemple :

$> ls -l | ./script.sh "ok"
John   ok_file
Mark   ok_test

Here is what the script looks like :

#!/bin/bash                                                                                                                                                                                   

while read hello
do
    name=$(echo $hello | cut -d' ' -f9 | grep $1)

    if [ $? = 0 ]
    then
        log=$(echo $hello | cut -d' ' -f3)
        echo -n $log' ' && echo $name
    fi
done

It works just fine but I am missing one condition : it doesn't display the file that STARTS with the parameter but any file that CONTAINS it.

How could I change this script to add this condition?

Thanks a lot.


Answer:

The 'grep' command uses a regular expression to match text. Use a '^' before the expression, to match from the start of the line. So, you can change the line

name=$(echo $hello | cut -d' ' -f9 | grep $1)

to

name=$(echo $hello | cut -d' ' -f9 | grep "^"$1)

and you should get the expected result.


Related:


How to replace newlines/linebreaks with a single space, but only if they are inside of start/end regexes?


regex,linux,shell,unix,replace
I have an extremely big (many GBs in size) file that looks like [x data1 data2 data3 data4 y] [a data5 data 6 data7 data 8 b> [x data y] ...and so on How to replace newlines (which might be surrounded by spaces) with a single space, but only if...

shell script to read each line and run the condition


shell
I have a log file which has the below data 120 140 200 110 200 200 120 90 100 I want to read this file and compare each line(number) with 200, and if it crosses 200 - then it has to compare the next word till the 5 consecutive which...

How to extract single-/multiline regex-matching items from an unpredictably formatted file and put each one in a single line into output file?


linux,shell,unix,replace,grep
I have a very huge file which looks like this: <a>text</a>text blah <b>data1</b>abc<b>data2</b> <b>data3</b>blahblah <c>text</c> <d>text</d> <x>blahblah<b>data4 data5 data6</b> <b>data7 </x> That is, its formatting is unpredictable. I need to extract each <b>...</b> item (it might contain multiline text!) and put every one of them in a single separate line....

Shell script to loop over files with same names but different extensions


linux,bash,shell
For example say I have: filename1.ext1 filename1.ext2 filename2.ext1 filename2.ext2 I need to write a shell script to feed these files into a program like so: program filename1.ext1 filename1.ext2 program filename2.ext1 filename2.ext2 Additionally the .ext1 files must be entered first and the .ext2 files second. Any help would be appreciated....

Matching string inside file and returning result


regex,string,bash,shell,grep
I've got a few peculiar issues with trying to search for a string inside of a .db file. The way I tried was by using grep, which does apparently find the string(s), although this is the output: $ grep "ext" *.db Binary file enormous.db matches There are a couple problems...

oh-my-zsh error after upgrade: ~/.oh-my-zsh/lib/misc.zsh:3: parse error near `then'


shell,zsh,zshrc,oh-my-zsh
today I upgrade my oh-my-zsh and then start this error: ~/.oh-my-zsh/lib/misc.zsh:3: parse error near `then' Inside this file there is this shell loop... ## Load smart urls if available for d in $fpath; do if [[ -e "$url/d-quote-magic"]]; then autoload -U url-quote-magic zle -N self-insert url-quote-magic fi done I think...

Redirect output from file to stdout


bash,shell,unix,stdout
I have a program that can output its results only to files, with -o option. This time I need to output it to console, i.e. stdout. Here is my first try: myprog -o /dev/stdout input_file But it says: /dev/ not writable I've found this question that's similar to mine, but...

Calling find more than once on the same folder tree


linux,bash,shell,unix,find
I'm running a find command multiple times on the same group of files. The results of my find commands are usually disjoint sets, AKA I'm running find -mmin +35; find -mmin -25, and doing different things to the results. It seems sort of silly to search through the entire file...

AWK write to new column base on if else of other column


linux,bash,shell,awk,sed
I have a CSV file with columns A,B,C,D. Column D contains values on a scale of 0 to 1. I want to use AWK to write to a new column E base in values in column D. For example: if value in column D <0.7, value in column E =...

How do I silence the HEAD of a curl request while using the silent flag?


bash,shell,curl,command-line,pipe
When I run the curl command and direct the data to a file, I get back the content of the site as expected. $ curl "www.site.com" > file.txt $ head file.txt Top of site ... However, this command shows a progress bar, which I do not want: % Total %...

Why can I view some Unix executable files in Mac OS X and not others?


git,bash,shell,unix,binary
I am on a Macbook Pro on Mac OS X 10.10 (Yosemite). When I go to /usr/bin, git is there as a unix executable file. When I open it up in Sublime Text, all I get is unreadable machine code. However, when I open up a different Unix executable file—in...

Piping echo into sendmail in rc.local fails


linux,shell,email,sendmail,boot
I'm trying to get my system to send me an email when the system boots. Using the following command works as intended: echo -e "TO:[email protected] \nSUBJECT:System Booted \n\nBeware I live! \n"$1 | sendmail -t -vs But if I add it to rc.local like so: #!/bin/sh -e # # rc.local #...

find numbers divisible by 3 in csv file using shell script


bash,shell,unix,awk
I have csv file having content like below : 1|2|3 4|5|6 7|8|9 Now I would like to find the numbers which are divisible by 3 using shell scripting. I would like to use awk command for this. I am learning shell scripting. So could you please help me out to...

Use Unix Executable File to Run Shell Script and MPKG File


osx,shell,unix
I have 2 shell scripts and 2 mpkg installer, I am trying to use an unix excitable file to run them all. here is the script I have, but it always has error message "No such file or directory" ? #!/bin/sh # Find the absolute script current path path=$( cd...

Missing one condition to display a string


shell
I am working on a small script that simply takes a parameter and read the output of ls -l and displays the user and name of the file that start with that parameter. For exemple : $> ls -l | ./script.sh "ok" John ok_file Mark ok_test Here is what the...

Storing data in shell


shell,sh
I have a need to store a list of e-mails in a shell script. This script will get called and passed a customer number. depending on the customer number I want to populate a variable based on the passed in customer number. I am not sure how to accomplish this...

How to test if a command is a shell reserved word?


bash,shell
I am writing a bash script and I would like to verify if a string is a shell reserved word (like if, for, alias, etc...). How can I can do this?...

How do I align my output?


shell
I'm writing a script that takes a parameter and reads the output of ls -l. It then displays the user and the name of the file STARTING with that parameter. For example : $> ls -l | ./script.sh "o" John ok_test RandomUser o_file My script works just fine, but I...

Check for decimal point and add it at the end if its not there using awk/perl


regex,perl,shell,awk
I have test.dat file with values given below: 20150202,abc,,,,3625.300000,,,,,-5,,,,,,,,,,,,,,,,,,,,,, 20150202,def,,,,32.585,,,,,0,,,,,,,,,,,,,,,,,,,,,, 20150202,xyz,,,,12,,,,,0.004167,,,,,,,,,,,,,,,,,,,,,, My expected output is shown below: 20150202,abc,,,,3625.300000,,,,,-5.,,,,,,,,,,,,,,,,,,,,,, ^. added here 20150202,def,,,,32.585,,,,,0.,,,,,,,,,,,,,,,,,,,,,, ^. added here 20150202,xyz,,,,12.,,,,,0.004167,,,,,,,,,,,,,,,,,,,,,, ^. added here So if column 6 and 11 doesn't have decimal point in it, then we should add '.' at the end of...

Export database from a remote server into a specific folder on my computer


php,mysql,database,shell,command-line-interface
I'm having trouble with a database export that is too big for phpmyadmin to handle. I'm curious what the syntax would be for exporting a mysql database from a remote server and getting the export file into a specific folder on my own computer, all using the command line. Say...

Using AWK to parse fields with commas


regex,bash,shell,awk
Edited - TLDR: Using awk to parse fields that include commas. # original config file - confile1 $ cat confile1 list=( app1,"HOSTNAME - port - application name - alert1",99.0,99.0 app2,"HOSTNAME - port - application name - alert1",99.0,99.0 app3,"HOSTNAME - port - service name - alert2",99.0,99.0 web1,"URL - HOSTNAMES(01,02) - http://someurl.com/...

linux - running a process and tailing a file simultaneously


bash,shell,tail
I want a run a long task on a remote machine (with python fabric using ssh). It logs to a file on the remote machine. What I want to do is to run that script and tail (actively display) the log file content until the script execution ends. The problem...

Need to update a text file with new content found in an other text file


bash,shell,text-files
hello i was update a file, removing some content from it, now that i have to new file i want it's content back to the original file hidden=Desktop/newtodo.txt do echo -e "$y \t $line" >> $hidden y=$(($y+1)) done newtodo.txt > todo.txt i have content in todo.txt i updated them in...

adb shell command from batch file


android,shell,adb
I have a .bat file that runs script for testing an app and printing the log to file i have all the commands i tested them manually. Problem: after entering the command adb shell, the shell opens in the command prompt. I wrote the next commands that are entered in...

how to deletes line from a text file that are taken from another file [duplicate]


shell,awk,sed,grep,sh
This question already has an answer here: Remove duplicates from text file based on second text file 4 answers I have a data.txt file with a lot of lines in it and a lines.txt that contains some lines. I want to delete all lines from data.txt that match any...

Fail Jenkins job when nosetests fail


python,shell,jenkins,continuous-integration
I am managing deployment / CI of a Flask app through Jenkins. I have a build step in the form of an executed shell which runs a shell script in the host that in turns runs nosetests. The Jenkins shell command looks like so: $WORKSPACE/ops/test_integration.sh integration.sh looks like so: SETTINGS_FILE="settings/integration.py"...

How to store command line arguments for future use?


bash,shell
I have a big bash script (version 3.2.51(1)-release) and I need to store command line arguments for use in the script. I checked many threads here and people suggested to use "[email protected]" to access all arguments. If I run this script (test.sh) - #!/bin/bash echo "[email protected]"; $> ./test.sh 1 2...

Linux-wget command


linux,shell,wget
I need a quick help on customizing my wget command in a shell script: The wget command looks something like this: wget http://infamvn:8081/nexus/content/groups/LDM_REPO_LIN64/com/infa/com.infa. products.ldm.ingestion.server.scala/10.0.0.135.527-SNAPSHOT/com.infa.products.ldm.ingestion.server.scala-10.0.0.135.527-20150622.210643-1-sources.jar Here I'd like to add the 10.0.0.135.527 in a variable, so I created a script something like this: n = 10.0.0.135.527 wget...

How to find average and maximum in an interval using Shell [closed]


linux,bash,shell,unix,awk
I would like to extract sum, mean and average in each 6 numbers interval from a column. I found many discussions related to this problem, but all those are for whole column. e.g. To compute sum of a column: awk '{sum+=$1} END { print sum}' To calculate Average: awk '{sum+=$1}...

using sed to replace a line with back slashes in a shell script


regex,bash,shell,ssh,sed
I am trying to replace the bottom one of these 2 lines with sed in a file. <rule>out_prefix=orderid ^1\\d\+ updatemtnotif/</rule>\n\ <rule>out_prefix=orderid ^2\\d\+ updatemtnotif/</rule>\n\ And the following command seems to do that when executed as a command at the bash prompt sed -i '[email protected]_prefix=orderid ^2\\\\d\\+ updatemtnotif/@out_prefix=orderid ^2\\\\d\\+ updatemtnotif_fr/@g' /opt/temp/rules.txt however, when...

Multiple line search in a file using java or unix command


java,shell,unix,command
I want to find the following pattern in a file like subclass "Pool1" 11:22:33:44:55:66 { dynamic; } the above pattern I have to find in a file. How can I find either using java or Unix command?...

Error while executing lsof from bash script


linux,bash,shell,scripting
I am trying to check for the process which are running on a particular port and if present kill them, as i have to start another process on that port. Below is my code snippet. #!/usr/bin/env bash TEST_CASE_PATH="$HOME/Projects"; #kill Previous Process Running on port 9130 OUTPUT="$(ls -1)" echo "${OUTPUT}" #...

how to spy on linux binaries for testing of shell scripts


linux,bash,shell,testing,spy
sorry if this is a duplicate, i had no idea what to search for... my use case is more complex, but can be narrowed down to the following problem: i want to run a bash script, which invokes all sorts of binaries, for example: grep. i want to assert that...

Delete some lines from text using Linux command


linux,shell,sed,grep,pattern-matching
I know how to match text using regex patterns but not how to manipulate them. I have used grep to match and extract lines from a text file, but I want to remove those lines from the text. How can I achieve this without having to write a python or...

shell script cut from variables


bash,shell,shellcode
The file is like this aaa&123 bbb&234 ccc&345 aaa&456 aaa$567 bbb&678 I want to output:(contain "aaa" and text after &) 123 456 I want to do in in shell script, Follow code be consider #!/bin/bash raw=$(grep 'aaa' 1.txt) var=$(cut -f2 -d"&" "$raw") echo $var It give me a error like...

Shell script to check if mysql is up or down


mysql,shell,cron
I want a bash shell script that i can run using a cron job to check if mysql on a remote server is running. If it is, then do nothing, other start the server. The cronjob will be checking the remote server for a live (or not) mysql every minute....

Passing GET variables to shell_exec in PHP


php,shell,get,mamp
I'm trying to run a shell script in PHP on my server that use youtube-dl to download a video. My code look like this: <form action="download.php" method="get"> <input type="text" name="link"><br> <input type="submit"> </form> and my download.php look like this: <?php $link = escapeshellarg($GET["link"]); $output = shell_exec('/Applications/MAMP/cgi-bin/youtube-dl ' .$link. ' 2>&1');...

Reading tab (\t) separated text from file in shell


bash,shell,text-files
my text file looks like this 1 get a 06-05-2000 2 get b 06-05-2001 3 get c 06-05-2002 4 get d 06-05-1442 5 get e 06-05-1998 6 get f 06-05-1909 I want to read what is after the first \t (tab) and store that in varA and what is after...

How to extract first letters of dashed separated words in a bash variable?


linux,string,bash,shell,variables
I would like to extract the first letter of dashed separated words value of my bash variable, like this: MY_TEXT=this-is-my-custom-text I would like to create a second variable like this: MY_INITIALS=timct...

Shell script file, checks if text file is empty or not


shell,text-files
I want to check if .todo.txt is empty or not. If is is empty, I want to add the echo in the file, and if it already has content, I want to do something else. I tried: hasData() { echo "Hello" } isEmpty() { echo -e "$n\t$comment\t$dueD/$dueM/$dueY " >> .todo.txt...

shell script to shutdown/restart Linux system


linux,shell
Is there any suitable shell script for shutting down or restarting a Linux machine? I have tried a shell script for shutdown, but when I enter sudo shutdown it will ask for the password. How we can enter the password using the script?

Using the shell provided with NodeJS


javascript,node.js,shell,require,node-modules
Once you've installed NodeJS, you'll have a executable in your computer named NodeJS which is a shell. I was wondering what can I do with that... here you're able to run JS code as, for example, in the browser's console, great. Now, is it possible to require modules in that...

Identifying when a file is changed- Bash


bash,shell,unix
So in my bash shell script, I have it running through a for loop. Inside the for loop, I use find "$myarray[i]" >> $tmp to look for a certain directory each time through the loop. Sometimes, it finds the variable in myarray[i] and sometimes it doesn't. When it does find...

AWK count number of times a term appear with respect to other columns


linux,shell,command-line,awk,sed
Given a CSV file: id, fruit, binary 1, apple, 1 2, orange, 0 3, pear, 1 4, apple, 0 5, peach, 0 6, apple, 1 How can i calculate for each unique values in fruit, the number of times the binary value =1 / number of occurences of that fruit...

docker run local script without host volumes


database,shell,docker,docker-compose
The goal is to add data to my database server containers of a multi-container web app from a download using curl once the database containers are running. I can do this from docker-compose.yml or from docker run independent of the web app, as long as I use host volumes. How...

Sqoop Export with Missing Data


sql,postgresql,shell,hadoop,sqoop
I am trying to use Sqoop to export data from HDFS into Postgresql. However, I receive an error partially through the export that it can't parse the input. I manually went into the file I was exporting and saw that this row had two columns missing. I have tried a...

Why does `su` not work in Jenkins?


shell,jenkins,su
su doesn't seem to work as intended when run in Jenkins build job script. su username1234 whoami returns jenkins My initial guess is that each line of the Jenkins shell script is a new shell. But doing repeated echo $$ on separate lines shows that's not the case. ...

Mac OSX - Allow for user input in shell script via GUI or Prompt


osx,bash,shell
first time post so please let me know how I could improve.. I created a shell script which requires a person to input their name and then generates a report. The script works as needed when chmod'd into an executable script and run from terminal. But, I would like to...

Group of data in shell language


linux,bash,shell
I wonder if the data in the group aren’t numbers ; the script works? When x="1 2 3" I can do a for loop: for i in $x I wonder if this is correct : read x read y xm=`cat $x` ym=`cat $y` zm="$xm $ym" for i in $zm do...

Allowing an executable to be terminated w/ ctrl+c in a shell script w/o exiting


bash,shell
Example: #!/bin/bash command_a # starting a executable command_b # should be executed after after exiting A A is exited via ctrl+C. I didn't really know how to search for this....