I am using sed -i ina while loop. But while replacing string it adds new line after matching - sed

I have a file that has some text like this:
AFG20-ENV284E2
AFG21-003
AFG22-189
PAK20-10092
PAK20-11742
PAK20-13467
PAK20-2903
I wanted to change them to
AFG20-ENV284E2|09-27-2020
AFG21-003|01-01-2021
AFG22-189|01-14-2022
PAK20-10092|09-03-2020
PAK20-11742|10-03-2020
PAK20-13467|11-05-2020
PAK20-2903|02-12-2020
I used sed in a while loop like this:
while read oldnames newnames; do sed -i "s/$oldnames/$newnames/g" edited.mcc.tre; done <replace-oldnames.txt
this works very well but it adds a new line after every matching string. How can I avoid the code to not add a newline after matching ??

Your Code is working perfectly.
See below
replace-oldnames.txt
AFG20-ENV284E2 AFG20-ENV284E2|09-27-2020
AFG21-003 AFG21-003|01-01-2021
AFG22-189 AFG22-189|01-14-2022
PAK20-10092 PAK20-10092|09-03-2020
PAK20-11742 PAK20-11742|10-03-2020
PAK20-13467 PAK20-13467|11-05-2020
PAK20-2903 PAK20-2903|02-12-2020
edited.mcc.tre
AFG20-ENV284E2
AFG21-003
AFG22-189
PAK20-10092
PAK20-11742
PAK20-13467
PAK20-2903
while read oldnames newnames; do sed -i "s/$oldnames/$newnames/g" edited.mcc.tre; done <replace-oldnames.txt
Output: cat edited.mcc.tre
AFG20-ENV284E2|09-27-2020
AFG21-003|01-01-2021
AFG22-189|01-14-2022
PAK20-10092|09-03-2020
PAK20-11742|10-03-2020
PAK20-13467|11-05-2020
PAK20-2903|02-12-2020

Related

Flutter \n\n not breaking lines unless hard-coded string

I can see that Flutter allows me to use "\n\n" in a string and it causes a line break to appear in a Text item:
final String answer = "This is my text.\n\n"
"Here is the 2nd line.";
This is my text.
Here is the 2nd line.
However, when I try to use content pulled from firebase, and set in a variable, the line break ("\n") is actually printed:
final String answer = faq['answer'];
Shows:
This is my text.\n\nHere is the 2nd line.
How can I get my "\n\n" to actually show up as line breaks?
Firestore doesn't support any escape sequences within string values. If you write "\n" in a string, you're going to get exactly that back when you read it.
So you can try something like this:
final String answer = (faq['answer'] as String).replaceAll("\\n", "\n");

Flutter Unicode Apostrophe In String

I'm hoping this is an easy question, and that I'm just not seeing the forest due to all the trees.
I have a string in flutter than came from a REST API that looks like this:
"What\u0027s this?"
The \u is causing a problem.
I can't do a string.replaceAll("\", "\") on it as the single slash means it's looking for a character after it, which is not what I need.
I tried doing a string.replaceAll(String.fromCharCode(0x92), "") to remove it - That didn't work.
I then tried using a regex to remove it like string.replaceAll("/(?:\)/", "") and the same single slash remains.
So, the question is how to remove that single slash, so I can add in a double slash, or replace it with a double slash?
Cheers
Jase
I found the issue. I was looking for hex 92 (0x92) and it should have been decimal 92.
I ended up solving the issue like this...
String removeUnicodeApostrophes(String strInput) {
// First remove the single slash.
String strModified = strInput.replaceAll(String.fromCharCode(92), "");
// Now, we can replace the rest of the unicode with a proper apostrophe.
return strModified.replaceAll("u0027", "\'");
}
When the string is read, I assume what's happening is that it's being interpreted as literal rather than as what it should be (code points) i.e. each character of \0027 is a separate character. You may actually be able to fix this depending on how you access the API - see the dart convert library. If you use utf8.decode on the raw data you may be able to avoid this entire problem.
However, if that's not an option there's an easy enough solution for you.
What's happening when you're writing out your regex or replace is that you're not escaping the backslash, so it's essentially becoming nothing. If you use a double slash, that solve the problem as it escapes the escape character. "\\" => "\".
The other option is to use a raw string like r"\" which ignores the escape character.
Paste this into https://dartpad.dartlang.org:
String withapostraphe = "What\u0027s this?";
String withapostraphe1 = withapostraphe.replaceAll('\u0027', '');
String withapostraphe2 = withapostraphe.replaceAll(String.fromCharCode(0x27), '');
print("Original encoded properly: $withapostraphe");
print("Replaced with nothing: $withapostraphe1");
print("Using char code for ': $withapostraphe2");
String unicodeNotDecoded = "What\\u0027s this?";
String unicodeWithApostraphe = unicodeNotDecoded.replaceAll('\\u0027', '\'');
String unicodeNoApostraphe = unicodeNotDecoded.replaceAll('\\u0027', '');
String unicodeRaw = unicodeNotDecoded.replaceAll(r"\u0027", "'");
print("Data as read with escaped unicode: $unicodeNotDecoded");
print("Data replaced with apostraphe: $unicodeWithApostraphe");
print("Data replaced with nothing: $unicodeNoApostraphe");
print("Data replaced using raw string: $unicodeRaw");
To see the result:
Original encoded properly: What's this?
Replaced with nothing: Whats this?
Using char code for ': Whats this?
Data as read with escaped unicode: What\u0027s this?
Data replaced with apostraphe: What's this?
Data replaced with nothing: Whats this?
Data replaced using raw string: What's this?

How to add escape character in string?

editedDate = `"16/11/2016 10:58:11"`
I want to send above string to server with escape character.
let revisedText = editedDate.replacingOccurrences(of: "/", with: "\\/")
Still i get value as
"16\\/11\\/2016 10:58:11"
I want
"16\/11\/2016 10:58:11"
The value is actually correct. Here is the output of the playground when printing the variable:
However, you may just have seen the value in the sidebar if you are using playground or in LLDB:
In that way, you can see the escape character is shown (\). Actually, it's not there. You can safely send the string to the server.
Your code is correct. Use print to show the modified string on console. Debugger or REPL will escape the output, too, e.g. lldb:
(lldb) po editedDate.replacingOccurrences(of: "/", with: "\\/")
"16\\/11\\/2016 10:58:11"
(lldb) p print(editedDate.replacingOccurrences(of: "/", with: "\\/"))
16\/11\/2016 10:58:11

Sed and awk application

I've read a little about sed and awk, and understand that both are text manipulators.
I plan to use one of these to edit groups of files (code in some programming language, js, python etc.) to make similar changes to large sets of files.
Primarily editing function definitions (parameters passed) and variable names for now, but the more I can do the better.
I'd like to know if someone's attempted something similar, and those who have, are there any obvious pitfalls that one should look out for? And which of sed and awk would be preferable/more suitable for such an application. (Or maybe something entirely else? )
Input
function(paramOne){
//Some code here
var variableOne = new ObjectType;
array[1] = "Some String";
instanceObj = new Something.something;
}
Output
function(ParamterOne){
//Some code here
var PartOfSomething.variableOne = new ObjectType;
sArray[1] = "Some String";
var instanceObj = new Something.something
}
Here's a GNU awk (for "gensub()" function) script that will transform your sample input file into your desired output file:
$ cat tst.awk
BEGIN{ sym = "[[:alnum:]_]+" }
{
$0 = gensub("^(" sym ")[(](" sym ")[)](.*)","\\1(ParameterOne)\\3","")
$0 = gensub("^(var )(" sym ")(.*)","\\1PartOfSomething.\\2\\3","")
$0 = gensub("^a(rray.*)","sA\\1","")
$0 = gensub("^(" sym " =.*)","var \\1","")
print
}
$ cat file
function(paramOne){
//Some code here
var variableOne = new ObjectType;
array[1] = "Some String";
instanceObj = new Something.something;
}
$ gawk -f tst.awk file
function(ParameterOne){
//Some code here
var PartOfSomething.variableOne = new ObjectType;
sArray[1] = "Some String";
var instanceObj = new Something.something;
}
BUT think about how your real input could vary from that - you could have more/less/different spacing between symbols. You could have assignments starting on one line and finishing on the next. You could have comments that contain similar-looking lines to the code that you don't want changed. You could have multiple statements on one line. etc., etc.
You can address every issue one at a time but it could take you a lot longer than just updating your files and chances are you still will not be able to get it completely right.
If your code is EXCEEDINGLY well structured and RIGOROUSLY follows a specific, highly restrictive coding format then you might be able to do what you want with a scripting language but your best bets are either:
change the files by hand if there's less than, say, 10,000 of them or
get a hold of a parser (e.g. the compiler) for the language your files are written in and modify that to spit out your updated code.
As soon as it starts to get slightly more complicated you will switch to a script language anyway. So why not start with python in the first place?
Walking directories:
walking along and processing files in directory in python
Replacing text in a file:
replacing text in a file with Python
Python regex howto:
http://docs.python.org/dev/howto/regex.html
I also recommend to install Eclipse + PyDev as this will make debugging a lot easier.
Here is an example of a simple automatic replacer
import os;
import sys;
import re;
import itertools;
folder = r"C:\Workspaces\Test\";
skip_extensions = ['.gif', '.png', '.jpg', '.mp4', ''];
substitutions = [("Test.Alpha.", "test.alpha."),
("Test.Beta.", "test.beta."),
("Test.Gamma.", "test.gamma.")];
for root, dirs, files in os.walk(folder):
for name in files:
(base, ext) = os.path.splitext(name);
file_path = os.path.join(root, name);
if ext in skip_extensions:
print "skipping", file_path;
else:
print "processing", file_path;
with open(file_path) as f:
s = f.read();
before = [[s[found.start()-5:found.end()+5] for found in re.finditer(old, s)] for old, new in substitutions];
for old, new in substitutions:
s = s.replace(old, new);
after = [[s[found.start()-5:found.end()+5] for found in re.finditer(new, s)] for old, new in substitutions];
for b, a in zip(itertools.chain(*before), itertools.chain(*after)):
print b, "-->", a;
with open(file_path, "w") as f:
f.write(s);

How do I send a Carriage Return with Windows Netcat?

I want to type directly into the Powershell prompt too, not pipe in a text file.
Foo`r doesn't work for me. For example:
echo "RJ`r`n" | .\nc.exe -u 192.168.1.247 2639
but what I'd really like to do is just
.\nc.exe -u 192.168.1.247 2639
then start typing into the prompt.
Try:
"Foo`ndoes work for me"
If you need a full CRLF sequence then:
"Foo`r`ndoes work for me"
Note that the escapes chars only work in double-quoted strings - not single quoted strings.
Update: Redirect in < is not supported in PowerShell at this time so you can only get stdin to the exe from the interactive prompt using the pipeline. Is it possible nc.exe is expecting another character (or escape char) to terminate the input? I know that console exes can receive stdin from PowerShell. If you have the C# compiler, you can see this by compiling the following source (csc echostdin.cs):
using System;
public class App
{
public static void Main()
{
int ch;
while ((ch = Console.In.Read()) != -1)
{
Console.Write((char)ch);
}
}
}
Then execute the exe:
PS> "foo`r`n" | .\echostdin.exe
foo
PS>