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 9 months ago.
Improve this question
Could someone tell me what Cg== means, I just know it's related to Base64.
I have searched but I still don't have a correct answer or an idea of what it is, and I don't have much knowledge about base64
Cg== is the base64 encode of the new line character in the latest position. So if you want to encode ABC you will get QUJD, however if you include a "return character" after ABC you will get QUJDCg==.
You can use hexdump or xxd to reveal the actual value of the character in hexadecimal. In the case of Cg==, it's a linefeed (0A) which can be verified with the following:
❯ echo -n "Cg==" | base64 -d | hexdump -C
00000000 0a |.|
00000001
In my experience Cg== arises from passing a string (usually credentials) to base64 using echo (without the -n switch and thus appending the default newline character at the end) rather than e.g. with printf.
Related
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
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
#--------------------------------------------------------
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 trying to insert NA between every occurrence of two tab characters immediately following each other in a text file. How can I do it with a sed command?
This might work for you (GNU sed):
sed ':a;s/\t\t/\tNA\t/g;ta' file
This covers all occurrances of \t\t throughout a file
Or if you prefer:
sed 's/\t\t/\tNA\t/g;s//\tNA\t/g' file
Like this:
sed 's/xx/xNAx/g' file
where you type x using Control-V TAB
Or, if you have GNU sed, you can type:
sed 's/\t\t/\tNA\t/g' file
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 9 years ago.
Improve this question
I have a huge text file(100GB) that requires editing a single line on linux.
Clearly this can't be done with a regular text editor.
Is there a way to do this? basically jumps to the nth line and then edit it and then save it back.
You can use the 'sed' stream editor to edit files of arbitrary size as it does not need to load the entire file in at once. for instance:
sed '54 s/[0-9][0-9]*/gone/' < file_in.txt > file_out.txt
will replace a number found on line 54 with the word 'gone'.
It also supports editing a file in place with the '-i' option, but I have never tried it on a hundred gigabyte file. No reason it shouldn't work.
If you known the exactly byte offset of the location to edit, and the edition does not change the length of the line, then you could fseek() to the line, read the line in, change it and then write out.
Suppose there is a 6000 line 'example.txt' and you want to change 3001th line to 'hello world'.
head -n 3000 example.txt > tmp.txt
echo 'hello world' >> tmp.txt
tail -n 2999 example.txt >> tmp.txt
mv tmp.txt example.txt
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.