Cut command with dynamic passing of delimiter and position values


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Cut command with dynamic passing of delimiter and position values
# 1  
Old 09-10-2018
Cut command with dynamic passing of delimiter and position values

Hi All,

We have a requirement of picking nth position value by using cut command. value would be delimited by any symbols. We have to pass delimited value and postition to get the value in a string.

ex.

Code:
echo "A,B,C,D,E" |cut -d "," -f3
echo "A|B|C|D|E"|cut -d "|" -f2

Kindly frame the unix command to to get both output in a single command by passing delimited value and position dynmically.

Thanks in advance.

Last edited by Scott; 09-10-2018 at 10:00 AM.. Reason: Please use code tags
# 2  
Old 09-10-2018
Moderator's Comments:
Mod Comment Thread title changed from "Cut command with dynamic passing of delimited and postition value" hoping to get a better chance of finding other threads with related topics.
# 3  
Old 09-10-2018
Here is a solution in awk. Pass delimiter plus required position.

Code:
$ cat kk230689 
awk -v A="$*" '
BEGIN { split(A, want)}
{ for (i in want) {
      p=substr(want[i],2)
      v=substr(want[i],1,1)
      if (split($0, flds, v) >= p)
         print flds[p]
  }
}'

$ cat infile
A,B,C,D,E
A|B|C|D|E

$ ./kk230689 ',3' '|2' < infile
C
B

# 4  
Old 09-11-2018
Thanks Chubler,

You have written both o/p in single query , but our requirement is to pass the delimited value and position as a paramter and get respective result.

Example:
Input parameter will receive deifference format.
option1 :
A,B,C,D,E,F
Option2 :
A|B|C|D|E

for the above option , unix command should be static by passing paramter as delimited and position.

my workaround code :

echo "A,B,C,D,E"|cut -d "," -f2
echo "A|B|C|D|E"|cut -d "|" -f3
"," -f2 - these value should be dynamic..

Can you plz help on this.
# 5  
Old 09-11-2018
If I read you correctly you wish to dynamically change the delimiter used by cut:
Code:
DELIM=,
echo "a,b,c,d,e" | cut -d "$DELIM" -f 3
DELIM="|"
echo "A|B|C|D|E" | cut -d "$DELIM" -f 2

Or is it a case you don't know whether the delimiter will be comma, a pipe or something else?
Code:
echo "A|B|C|D|E" | tr '|;' ',,' | cut -d "," -f 2
echo "a;b;c;d;e" | awk -F"[,|;]" '{print $3}'

All the above are simplistic examples.

Andrew
This User Gave Thanks to apmcd47 For This Post:
# 6  
Old 09-11-2018
Why not a function with what you already have?


Code:
CUTDLFLD() { cut -d"$1" -f"$2" ${3:--}; }


Call like
Code:
echo "a,b,c,d,e" | CUTDLFLD "," "3"

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Problem in using cut command with pipe as a delimiter while using in a script

There is a text file in my project named as "mom.txt" in which i want to have contents like.................. LSCRM(Application Name): 1: This is my first application. 2: Today we did shell scripting automation for this app. 3: It was really a good fun in doing so. 4: Really good.| (Here i... (7 Replies)
Discussion started by: Abhijeet Anand
7 Replies

2. Shell Programming and Scripting

Passing an argument to cut command

Can we pass an argument to cut command as below Suppose cut command is used in for or while loop and we need to pass the incremental counter cut -f$i Here $i is an argument. Like wise it has to come cut -f1 cut -f2 Where i=1,2,3,.... (1 Reply)
Discussion started by: bashamsc
1 Replies

3. Shell Programming and Scripting

How to cut by delimiter, and delimiter can be anything except numbers?

Hi all, I have a number of strings like below: //mnt/autocor/43°13'(33")W/ and i'm trying to get the numbers in this string, for example 431333 please help thanks ahead (14 Replies)
Discussion started by: sunnydanniel
14 Replies

4. UNIX for Dummies Questions & Answers

set output delimiter as tab in cut command

I can not make it work, it prints \t rather than introduce tabs. cut -d "," -f 4,8 Samples.csv --output-delimiter="\t" | sort > out Since I am running this command within a shell script, I tried manually inserting tab in this command, still does not work. I am using bash shell Suggestions... (8 Replies)
Discussion started by: analyst
8 Replies

5. Shell Programming and Scripting

CUT command delimiter in reverse

Hi, I've a situation where, a=xxx.yyy.zzz.txt EXTN=`echo $a | cut -d . -f2` Using the above code it delimites and will return "yyy.zzz.txt" to EXTN. But i need to get only the extension "txt". so as per the above code it delimits in the first "." itself. Can anyone help how to do... (6 Replies)
Discussion started by: skcvasanth
6 Replies

6. Shell Programming and Scripting

passing variable values to awk command

Hi, I have a situation where I have to specify a different value to an awk command, I beleive i have the gist of this done, however I am not able to get this correct. Here is what I have so far echo $id 065859555 This value occurs in a "pipe" delimited file in postition 8. Hence I would... (1 Reply)
Discussion started by: jerardfjay
1 Replies

7. Shell Programming and Scripting

passing a list of dynamic names to a "PS" command in shell script?

Hi, I am new to shell script. This is my first post .I have written a small script which returns list of names starts with "ram" in /etc/passwd .Here is that:- #!/bin/ksh NAME_LIST="name_list.txt" cat /dev/null > $NAME_LIST evalcmd="cat /etc/passwd | grep "^ram?*" | cut -d: -f1" eval... (3 Replies)
Discussion started by: sachin.tendulka
3 Replies

8. UNIX for Advanced & Expert Users

How can I use double character delimiter in the cut command

Hi All, Can the cut command have double character delimiter? If yes, how can we use it. if my data file contains : apple || mango || grapes i used cut -f1 -d"||" filename but got an error. Plz help.... Thanks. (1 Reply)
Discussion started by: AshishK
1 Replies

9. UNIX for Dummies Questions & Answers

scripting: multiple values from file passing to command

one of my colleagues has this question. he has a command, C_CMD which accepts 4 variables, $1 $2 $3 $4 he wants to load up a file with multiple rows, one row per set of variables and then iteratively execute the command based on the content of the file. example: at the command line you'd... (5 Replies)
Discussion started by: LisaS
5 Replies

10. Shell Programming and Scripting

Dynamic delimiter in cut command

hello.. my prob is as follows: i have to read from a file which may have different formats depending upon the delimiter used in the data of the file.now i need that the user input the delimiter.the input delimiter is stored in a variable and is used on cut command to retrieve necessary... (3 Replies)
Discussion started by: tej.buch
3 Replies
Login or Register to Ask a Question