Selecting strings with - as first character in bash


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Selecting strings with - as first character in bash
# 1  
Old 03-30-2012
Selecting strings with - as first character in bash

I have a variable containing a list of strings, and want to create a string with the first character being -.

Example:
Code:
var="-name fred paul -surname winnett dimech"

I want a string, namely
Code:
errStr="-name -surname"

# 2  
Old 03-30-2012
try this
Code:
errStr=`echo $var | tr  -s " " "\n" | grep - | tr "\n" " "`

# 3  
Old 03-30-2012
Quote:
Originally Posted by kristinu
I have a variable containing a list of strings, and want to create a string with the first character being -.

Example:
Code:
var="-name fred paul -surname winnett dimech"

I want a string, namely
Code:
errStr="-name -surname"


There's no need for any external command:
Code:
var="-name fred paul -surname winnett dimech"
errStr=
for word in $var
do
  case $word in
    -*) errStr=${errStr:+"$errStr "}$word ;;
  esac
done

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Escape bash-special character in a bash string

Hi, I am new in bash scripting. In my work, I provide support to several users and when I connect to their computers I use the same admin and password, so I am trying to create a script that will only ask me for the IP address and then connect to the computer without having me to type the user... (5 Replies)
Discussion started by: arcoa05
5 Replies

2. Shell Programming and Scripting

How to remove certain character strings with awk?

Hi all, I need to remove DBPATH= and /db from the string below using awk (or sed, as it also exists on the machine). Input: DBPATH=/some/path/database/db Desired output: /some/path/database Thank you! (8 Replies)
Discussion started by: ejianu
8 Replies

3. Shell Programming and Scripting

Using arrays in bash using strings to bash built-in true

I have the following code and for some reason when I call the program using /home/tcdata/tatsh/trunk/hstmy/bin/bash/raytrac.bash --cmod=jcdint.cmod I get hasArgument = hasArgument = true Somehow the array element is returning even though I have not chosen the option. ... (41 Replies)
Discussion started by: kristinu
41 Replies

4. UNIX for Dummies Questions & Answers

Reading character by character - BASH

Hello every one and thanks in advance for the time you will take to think about my problem. I would like to know if it's possible (in BASH) to read a text file character after character. Exactly this is what I would like to do : Txt file : ATGCAGTTCATTGCCAAA...... (~2.5 millions... (3 Replies)
Discussion started by: sluvah
3 Replies

5. UNIX for Dummies Questions & Answers

character-by-character comparison of strings

This might be a dummy question, but is there a command in UNIX that compare two strings character-by-character and display the difference? ---------- Post updated at 11:25 AM ---------- Previous update was at 10:32 AM ---------- Or probably what I'm looking is how to break a string into... (3 Replies)
Discussion started by: Orbix
3 Replies

6. Shell Programming and Scripting

Bash folder manipulation - selecting/copying specified files

Bash/scripting newbie here - I feel this might be a trivial problem, but I'm not sure how to tackle it. I've got a folder of a year's worth of files, with some random number of files generated every day of the year (but at least one per day). I'm writing a script to automatically grab the file with... (6 Replies)
Discussion started by: WildGooseChased
6 Replies

7. Shell Programming and Scripting

bash script for selecting grib files with cdo

hi, i am new with scripts and have a little problem. i have a file with dates in the form YYYYMMDD, in average about 40 days per year. now i shall cut those days with cdo selday from monthly files. the script shall look up the days, forward them to the cdo operator and write the new grib... (1 Reply)
Discussion started by: jumkae
1 Replies

8. UNIX for Dummies Questions & Answers

Getting strings before and after a character

OK This one has me stumped. I have the following line, program name - the program description that can also contain a hyphen - character. I'm need to separate the "program name" from the program description. I've tried using an array function with the - as delimiter, but I ran into a... (2 Replies)
Discussion started by: ricksj
2 Replies

9. UNIX for Dummies Questions & Answers

Help to replace character strings

Hello Can Any1 help me. I want to replace a specific character string inside a file at a specific location with a particular character with the help of a command or a shell script. The tr command replaces a specific character with another for all the occurences of that character in the file. I... (5 Replies)
Discussion started by: rahulrathod
5 Replies

10. Shell Programming and Scripting

Finding character mismatch position in two strings

Hello, I would like to find an efficient way to compare a pair of strings that differ at one position, and return the difference and position. For example: String1 123456789 String2 123454789 returning something - position 6, 6/4 Thanks in advance, Mike (5 Replies)
Discussion started by: etherite
5 Replies
Login or Register to Ask a Question