Skip to main content
23 events
when toggle format what by license comment
Jul 28, 2022 at 6:06 comment added jubilatious1 What is your criteria for "copying over" to a new file? Your example refers to searchterm as the starting point of what you want to copy, and then there's some unknown criteria in the second line that makes you decide to copy it over (or not). If you could tease out that second criteria, there's your solution.
Jul 27, 2022 at 23:47 answer added jubilatious1 timeline score: 0
Jul 27, 2022 at 19:45 answer added Quarlington timeline score: 0
Jul 25, 2022 at 22:52 history edited MJiller CC BY-SA 4.0
added 995 characters in body
Jul 25, 2022 at 18:49 history edited MJiller CC BY-SA 4.0
added 200 characters in body
Jul 25, 2022 at 18:19 history edited MJiller CC BY-SA 4.0
added 429 characters in body
Jul 25, 2022 at 16:42 comment added MJiller Testing shows the awk command just blindly copies all matching couplets over the file2.txt, i.e., almost all lines contained in file1.txt. But I need it to offer the user an option of which couplets to copy,. Of the roughly 2000 couplets contained in file1.txt, I only want to copy over maybe 400. So can awk present the option whether to copy or not?
Jul 25, 2022 at 14:00 history edited MJiller CC BY-SA 4.0
added 51 characters in body
Jul 25, 2022 at 13:54 history edited MJiller CC BY-SA 4.0
added 51 characters in body
Jul 25, 2022 at 13:34 history edited MJiller CC BY-SA 4.0
added 12 characters in body
Jul 25, 2022 at 12:42 history edited MJiller CC BY-SA 4.0
added 272 characters in body
Jul 25, 2022 at 12:40 comment added MJiller I'm thinking of bash because, though my understanding of it is fairly limited, I do at least have some basic comprehension of its workings. I figured awk would be a choice for this as well, but it's workings are a bit of a black box to me. That said, I'm open to these sorts of suggestions--thanks.
Jul 25, 2022 at 12:36 history edited MJiller CC BY-SA 4.0
added 272 characters in body
Jul 25, 2022 at 12:30 history edited MJiller CC BY-SA 4.0
added 272 characters in body
Jul 25, 2022 at 6:44 comment added r_31415 Do you need to do it in bash? I think awk would be a better option (e.g. awk '/searchterm/ { getline var; print $0"\n"var > "file2.txt" }' infile)
Jul 25, 2022 at 6:12 comment added Philippos I suggest you start with a while read loop, try to write that script and come back with your problem if you don't succeed.
Jul 25, 2022 at 0:21 comment added MJiller No. searchterm is always the same and appears in the line preceding, but there is some variation in what follows searchterm in the line in which it appears. The second line that is to be included when copying to file2.txt has minor variations in it too. searchterm appears consistently in every other line off the file
Jul 24, 2022 at 23:55 comment added Sotto Voce Does the string to be searched for come from the 2nd #searchterm xxxx line? So the term being searched for changes as the code advances through the file?
Jul 24, 2022 at 23:54 comment added MJiller The aim is that both the line containing searchterm and the line following get copied to file2.txt
Jul 24, 2022 at 23:49 history edited MJiller CC BY-SA 4.0
added 3 characters in body
Jul 24, 2022 at 23:44 comment added MJiller Pretty much that, yeah: copy 2 lines. Although I know that searchterm appears in every other line, in just that form, as in the sample given. Which is why grep -A 1 works to get the 2 lines needed, or couplet. Hope that makes sense.
Jul 24, 2022 at 23:34 comment added Sotto Voce "couplet" isn't a common term when talking about searching for strings in files. Are you saying you want to read two lines from a file, check whether a string appears in either line, and decide whether to write the lines/string to an output file?
Jul 24, 2022 at 23:31 history asked MJiller CC BY-SA 4.0