using printf to create a header [closed] - linux

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am very new to this, so my apologies in advance if this is a simple question.
I would like to create an output using 'printf' that would look like below:
#-------------------------------------------------------
#TEXT1 #TEXT2
#--------------------------------------------------------
I would really appreciate if someone could give me some hints as to how to do this.

Here's one way using long strings with a specific number of characters.
dashes=$(printf "%0.s-" {1..55})
printf "#$dashes\n#TEXT1%32s#TEXT2\n#$dashes-\n" " "
See Bash-Hackers Wiki for detailed information on printf command in bash.
How it works
dashes=$(printf "%0.s-" {1..55}) - uses brace expansion and command substitution to create a string variable of 55 consecutive - characters.
\n - prints a newline character
%32s - prints 32 " " characters
Update
To print three tabs between #TEXT1 and #TEXT2:
dashes=$(printf "%0.s-" {1..55})
printf "#$dashes\n#TEXT1\t\t\t#TEXT2\n#$dashes-\n" " "
\t represents a tab character.

Just print the strings with newlines at the end.
printf '#-------------------------------------------------------\n'
printf '#TEXT1 #TEXT2\n'
printf '#--------------------------------------------------------\n'
You can also use echo, since there's no formatting in the strings. Then you don't need \n at the end.

You could try this,
$ printf "#-------------------------------------------------------\n#TEXT1 #TEXT2\n#--------------------------------------------------------\n";
#-------------------------------------------------------
#TEXT1 #TEXT2
#--------------------------------------------------------

Related

How to remove set of matching characters at the end of the string in a shell scripts [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 months ago.
Improve this question
I want to remove set of matching characters at the end of the string in a shell script. It should work in all the linux flavours, ideally with out using tools like sed,awk.
I found some examples on web but all of them are about removing a single character type.
Below is a set of examples which shows what I am trying to achieve.
Please help.
1. Input : test_-
Output: test
2. Input: test-_-
Output: test
3. Input: test1__-
Output: test1
I want to remove the all the "hyphen" and "underscore" characters from the end of the string.
Since you are tagging this zsh:
Assuming that your string is stored in a variable input, you can do a
if [[ $input =~ ^((.*[^-_])) ]]
then
output=$MATCH
fi
The .* does a greedy match, which guarantees that the last character is neither a dash nor a hyphen.
In bash, this works similar, only that you have to set
output=${BASH_REMATCH[1]}
Supposing your data is in a file, like
test_-
test-_-
test1__-
with grep
grep -oP '[a-z]*[0-9]*' data.txt

Replace with sed on csv file [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I have a csv file and I am trying to substitute the last letter for a word...
The input is
1111;AAA;... (more columns);A1a;A
2222;XXX;... (more columns);T3g;B
... (more rows)
...(more rows)
4564;AdA;... (more columns);G1a;A
33321;B1X; ... (more columns);T3g;B
And I want to replace A for "Avocado" and B for "Banana"...
I tried
#sed -e "s/;A$/;C/g" file.csv
But doesn't work, any advice, please?
Is the following what you're trying to achieve?
tink#host:~/tmp$ sed 's/A$/Avocado/;s/B$/Banana/' file.csv
1111;AAA;... (more columns);A1a;Avocado
2222;XXX;... (more columns);T3g;Banana
... (more rows)
...(more rows)
4564;AdA;... (more columns);G1a;Avocado
33321;B1X; ... (more columns);T3g;Banana
If that looks correct, and you want to change in-file, add a -i to sed.
If you want a new file, add a > new_file to the end of the line.
This seems to work:
sed -i 's/A$/Avocado$/g' file.csv
sed -i 's/B$/Banana$/g' file.csv
The -i replaces the text and the Regex doesn't need the ; because it should use only one character, right? Therefore, one can just specify that character and replace it with a whole word.

How can I delete the numbers from the output, that has same numbers in it? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
A nice user solved my problem, so I changed my question, now its not the same as before the other one was.
My permutation look like this now:
x=3
for i in $(eval echo "{1..$x}{1..$x}{1..$x}"); do
echo "$i"
done
How can I get just these numbers (123,132,231,213,321,312) from the output with two "for" periods and with "seq" command?
if you want to be able to dynamically set 3 in {1..3} something like {1..$x}. If you try that you will experience that the output will be {1..3} and not the expected 1 2 3. This is because braces are expanded before variables.
What you need to use is eval echo "{1..$x}" which will indeed output 1 2 3. And in a for loop you could use a command substitution:
x=3
for i in $(eval echo "{1..$x}{1..$x}{1..$x}"); do
echo "$i"
done

How to remove specific characters from a string? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am brand new to Perl and struggling with it.
I need to learn just the basics and not much.
I googled and got no simple answer to my question.
I have a string which contains numbers, dots, dashes, colon: and two alphabets.
I want to replace one alphabet by a space and the other by nothing.
How do I do this?
Is there no string.ReplaceChar(theChar, replacement)?
You can try as in the example below:
From commandline:
To replace only the first occurrence of the alphabets:
sdlcb#ubuntu:~$ echo "123.-A456:7B9AB0" | perl -pe 's/A//; s/B/ /'
123.-456:7 9AB0
To replace all occurrences of the alphabets:
sdlcb#ubuntu:~$ echo "123.-A456:7B9AB0" | perl -pe 's/A//g; s/B/ /g'
123.-456:7 9 0
Within script:
#!/usr/bin/perl -w
use strict;
my $data = "123.-A456:7B9AB0";
my $final_data = $data;
$final_data =~ s/A// ;
$final_data =~ s/B/ /;
print "data: $data\n";
print "final_data: $final_data\n";
Use g for substituting all occurrences.
Perl lends itself to using regular expressions, so that is how I would approach it;
#!/usr/bin/perl -w
use strict;
my $ALPHABET = 'abcde';
my $source_data = "1.2.3-$ALPHABET:$ALPHABET";
my $dest_data = $source_data
$dest_data =~ s/(-)$ALPHABET(:)/$1 $2/;
print "source_data: $source_data\n";
print "dest_data: $dest_data\n";
--->
source_data: 1.2.3-abcde:abcde
dest_data: 1.2.3- :abcde
Here the reqular expresion operator (=~) is substituting the first occurance of the pattern (-)$ALPHABET(:) with a '- :' string.
This pattern is using capture groups '()' to locate the match within the data.
Depending on your data you will likely need to adjust the patterns in the capture groups.
The backrefernces $1 and $2 within the match are used for demonstration purposes.
We could help with a more specific regex if you include example data in your question.

How to add a character in front of multiple words in linux [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Here is a text file containing many words,each is separated by space breaks or line breaks.
Now I want to add a character,like "#" "$" "#" in front of each of them,
and I found doing this job one by one will take too much time,
are there any better ways,in bash?
Try using sed
sed -r 's/([^ ]+)/#\1/g' file
Or more concisely,
sed -r 's/[^ ]+/#&/g' file
Sample input
abc def pqr-stu xyz
Output
#abc #def #pqr-stu #xyz
Using sed, you could say:
sed 's/\b\w/#&/g' inputfile
This would append # before every word.

Resources