Calling specific characters from a find variable


Login or Register for Dates, Times and to Reply

 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Calling specific characters from a find variable
# 1  
Calling specific characters from a find variable

I'm trying to do something like this:

Code:
find . -name blablabla -exec  ln -s ./"{:53:14} blablabla" \;

The idea is find blablabla and create a symbolic link to it using part of it's path and then it's name, "blablabla."

I just don't know if I can call characters out of a find variable. Obviously the above doesn't work.

Basically, it'll be creating sylinks for every directory named blablabla like this:

Code:
hd_90_050 blablabla
hd_67_010 blablabla
hd_10_777 blablabla

Any help is appreciated.

Moderator's Comments:
Mod Comment Please wrap your samples in CODE TAGS by typing [CODE]your commands/sample of input/sample of output[/CODE] in your posts.

Last edited by RavinderSingh13; 01-24-2019 at 11:45 PM..
# 2  
With what you have shown us there is little hope of us being able to help you.

You seem to be saying that you want to extract 14 characters starting in position 53 from the 11 character string "./blablabla". We have no idea whether you are expecting other characters to come from subdirectory names, or if you have wildcard characters in "blablabla" that you aren't showing us, or something else.

If you tell us what operating system you're using, what shell you're using, show us some actual samples of the pathnames being produced by the command find . -name blablabla (in CODE tags), and show us exactly what ln commands you hope to produce from those pathnames (in CODE tags); we would have a much better chance of coming up with something that might accomplish what you're trying to do. Note that an ln -s command needs two operands; not just one (and your sample code seems to be trying to only produce one).

Please help us help you.
This User Gave Thanks to Don Cragun For This Post:
# 3  
I honestly do not quite understand what it is about, but can it help?
Code:
find $(pwd) -name "blablabla" | while read d; do ln -s $d ${d//\//_}; done

# 4  
Ah, yes I see. Your assumptions are right. I do expect a long string to be returned from find, and I do need to extract characters from that long path and pass them to the ln command.
I'm using a bash shell on CentOS Linux.

Code:
find . -name blablabla -exec  ln -s  {} ./"{:53:14} blablabla" \;

I think that command is more like what I need. Find blablabla and create a symlink in this directory with a prefix of part of it's path.
The moderator put my desired results in "code," in the original post which made the post quite confusing. In my opinion.

Each folder needs the unique characters from it's path because each directory "blablabla" will have the exact same name.

I don't think the code suggested by nezabudka will work. I don't see it extracting characters :53:14 and piping them to ln.

Hopefully, I've cleared things up a bit.
Thanks in advance.
# 5  
Give us some data to work upon - input data and desired results.
Your find command will definitely NOT work. Parameter expansion can't be used the way you wrote it.
This User Gave Thanks to RudiC For This Post:
# 6  
scribling
I showed a variant with a string variable from which you can extract any part of the substring
Code:
do ln -s $d ${d:5:4}

or change in this way
LESS=+/"Parameter Expansion" man bash


--- Post updated at 20:04 ---

And even so ))
Code:
do ln -s $d "./${d:5:4} "$(basename $d)

This User Gave Thanks to nezabudka For This Post:
# 7  
Quote:
Originally Posted by scribling
Ah, yes I see. Your assumptions are right. I do expect a long string to be returned from find, and I do need to extract characters from that long path and pass them to the ln command.
I'm using a bash shell on CentOS Linux.
That is good to know and will help us give you suggestions that will work in your environment IF you give us some representative sample data to use to verify that we understand what you're trying to do and to verify that the code we suggest will produce the results you want.

Quote:
Code:
find . -name blablabla -exec  ln -s  {} ./"{:53:14} blablabla" \;

I think that command is more like what I need. Find blablabla and create a symlink in this directory with a prefix of part of it's path.
The code seems to be you're trying to extract 14 characters from the pathnames find finds, but your sample data shows that you only expect to get 9 characters. We can't come up with any sample data that will trim the 14 characters you could extract from any pathname that would create the 9 characters shown in the sample output you say you want to produce.

And, as has already been said three times before in this thread, the find utility does not perform variable expansions on pathnames it outputs as -exec primary operands. The above find command will attempt to link each found pathname to the literal name {:53:14} blablabla. I'm sure that isn't what you want, but your refusal to give us a self-consistent description of what you're trying to do and your refusal to give us representative sample data that you will be processing leaves us confused and only able to make wild guesses at what you really want to do.

Quote:
The moderator put my desired results in "code," in the original post which made the post quite confusing. In my opinion.

Each folder needs the unique characters from it's path because each directory "blablabla" will have the exact same name.
The moderator who added the CODE tags to post #1 in this thread for you did not in any way add any confusion to what you had in your original post. Not having CODE tags makes leading and trailing <space> and <tab> characters disappear from view and coalesces sequences of <space> and <tab> characters in the middle of a line to single <space> characters. If you had had sequences of <space>s in your sample output, the CODE tags would have made that clear for us and then we might have been able to make sense of your sample output. Unfortunately, with or without the CODE tags, your sample desired output doesn't fit the description of the problem you say you're trying to solve.

Note also that this is the first time you've said anything about blablabla being a directory. There is nothing in your find command that restricts the pathnames it returns to be pathnames of directories. I have no idea whether or not this might cause you problems with the results your code will produce, but if it is a constraint on the output you want to produce, it would have been helpful to know this up front.

Quote:
I don't think the code suggested by nezabudka will work. I don't see it extracting characters :53:14 and piping them to ln.

Hopefully, I've cleared things up a bit.
Thanks in advance.
Unfortunately, you haven't.

I'm guessing that nezabudka has come closest to guessing at what you're trying to do, but her code won't quite work for you yet. Hopefully, it is close enough that you can work out the rest. If not, you MUST give some concrete actual sample pathnames that the find command I showed you in post #2 in this thread produces as output AND show us the exact ln commands you hope to produce from each of those sample pathnames.

Please help us help you.
This User Gave Thanks to Don Cragun For This Post:
Login or Register for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #825
Difficulty: Medium
The term API seems to appear for the first time in the article of Ira W. Cotton, Data structures and techniques for remote computer graphics, published in 1968.
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Find records with specific characters in 2 nd field

Hi , I have a requirement to read a file ( 5 fields , ~ delimited) and find the records which contain anything other than Alphabets, Numbers , comma ,space and dot . ie a-z and A-Z and 0-9 and . and " " and , in 2nd field. Once I do that i would want the result to have field1|<flag> flag can... (2 Replies)
Discussion started by: ashwin3086
2 Replies

2. Shell Programming and Scripting

Calling a variable of variable from a file

Hi All, I have file which have looks like below abc=${def} def=${efg} efg= "this is the actual value" based on "abc" value I have to call "efg" value , Am using below lines but it is not working #!/bin/bash source file.txt echo $abc Please wrap all code, files, input &... (5 Replies)
Discussion started by: Prashanth.K
5 Replies

3. Shell Programming and Scripting

Find and replace with 0 for characters in a specific position

Need command for position based replace: I need a command to replace with 0 for characters in the positions 11 to 20 to all the lines starts with 6 in a file. For example the file ABC.txt has: abcdefghijklmnopqrstuvwxyz 6abcdefghijklmnopqrstuvwxyz abcdefghijklmnopqrstuvwxyz... (4 Replies)
Discussion started by: thangabalu
4 Replies

4. Shell Programming and Scripting

Regex in sed to find specific pattern and assign to variable

(5 Replies)
Discussion started by: radioactive9
5 Replies

5. Shell Programming and Scripting

Calling a Variable based on a Variable

Hi all, I have a source config file with variables like so: eth1_ip=192.168.1.99 eth2_ip=192.168.1.123 eth3_ip=172.16.1.1 I am trying to run a script which loops based on the number of eth interfaces on a machine and therefore modifies the variable it calls in the environment based on the... (5 Replies)
Discussion started by: landossa
5 Replies

6. Shell Programming and Scripting

Count specific characters at specific column positions

Hi all, I need help. I have an input text file (input.txt) like this: 21 GTGCAACACCGTCTTGAGAGG 50 21 GACCGAGACAGAATGAAAATC 73 21 CGGGTCTGTAGTAGCAAACGC 108 21 CGAAAAATGAACCCCTTTATC 220 21 CGTGATCCTGTTGAAGGGTCG 259 Now I need to count A/T/G/C numbers at each character location in column... (2 Replies)
Discussion started by: thienxho
2 Replies

7. Shell Programming and Scripting

Can't figure out how to find specific characters in specific columns

I am trying to find a specific set of characters in a long file. I only want to find the characters in column 265 for 4 bytes. Is there a search for that? I tried cut but couldn't get it to work. Ex. I want to find '9999' in column 265 for 4 bytes. If it is in there, I want it to print... (12 Replies)
Discussion started by: Drenhead
12 Replies

8. Shell Programming and Scripting

sed replacing specific characters and control characters by escaping

sed -e "s// /g" old.txt > new.txt While I do know some control characters need to be escaped, can normal characters also be escaped and still work the same way? Basically I do not know all control characters that have a special meaning, for example, ?, ., % have a meaning and have to be escaped... (11 Replies)
Discussion started by: ijustneeda
11 Replies

9. Shell Programming and Scripting

Find and replace a string a specific value in specific location in AIX

Hi, I have following samp.txt file in unix. samp.txt 01Roy2D3M000000 02Rad2D3M222222 . . . . 10Mik0A2M343443 Desired Output 01Roy2A3M000000 02Rad2A3M222222 . . (5 Replies)
Discussion started by: techmoris
5 Replies

10. Shell Programming and Scripting

calling a aliased variable

Issue: i have variable A which is an alias for variable B which is equal to "THIS IS A TEST" when every i echo variable A i only get the alias name for variable B, NOT the contents of variable B. HOSTNAME# echo $TESTIT + echo THIS IS A TEST THIS IS A TEST HOSTNAME# ls -l total... (10 Replies)
Discussion started by: Optimus_P
10 Replies

Featured Tech Videos